Razor syntax PHP equivalent

13,375

Solution 1

Thanks @mindplay.dk for linking to the Razor View Renderer for the Yii Framework! I wanted to share a recent find, Twig (http://www.twig-project.org/) as an alternative if you are looking for a standalone template engine for PHP. It's not Razor syntax, but it is simple and extensible.

Here's some examples from the site:

For Each:

{% for user in users %}
  * {{ user.name }}
{% else %}
  No user has been found.
{% endfor %}

Blocks & Inheritance:

{% extends "layout.html" %}

{% block content %}
  Content of the page...
{% endblock %}

Filters:

{{ "now"|date("m/d/Y") }}

{{ "I like %s and %s."|format(foo, "bar") }}
returns: I like foo and bar. (if the foo parameter equals to the foo string)

I'm still doing some preliminary development & testing with this engine and I'm liking it thus far!

Solution 2

There is a Razor-like view-engine for the Yii framework:

http://www.yiiframework.com/extension/razorviewrenderer

It's very simple - it doesn't seem to have any real Yii dependencies, so I can't imagine it would be very difficult to pull this out of Yii and use it in a different context.

Mind you, this is just a Razor-style template parser - it compiles Razor-style templates into plain vanilla PHP scripts. It relies on Yii for the actual view-engine.

Solution 3

Laravel's Blade Template Engine, uses a similar syntax to Razor. https://laravel.com/docs/master/blade

Solution 4

The closest you'll find is inside Fat-Free Framework's template engine, but it requires you to use curly braces. Instead of PHP's verbose <?php echo $x['y']['z']; ?> or the short tag-equivalent <?=$x['y']['z']?>, Fat-Free uses {@x.y.z}

Solution 5

I just had to look "Razor syntax" up and it seems nice enough for ASP.NET. In PHP though I would apply some sort of template framework (I usually use Smarty myself) to get some nice clean looking HTML-pages with only a minimum of control structure and variable referencing.

Share:
13,375
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin almost 2 years

    Is there an equivalent to the new ASP.NET razor syntax in PHP?

  • Russell Dias
    Russell Dias over 13 years
    Your first example is missing a ; after the string assignment.
  • Chris Laplante
    Chris Laplante over 13 years
    @Russell Dias - Thank you, I didn't spot that!
  • mindplay.dk
    mindplay.dk about 13 years
    whenever somebody says "Smarty", I have to post two links: one to the fat-free alternative, Dwoo - and one to my own ultra-thin engine, Outline ... I'm not just shamelessly plugging my own engine here - Smarty is a dinosaur, and there are much better alternatives to templating with PHP...
  • pqsk
    pqsk over 10 years
    My php is rusty, but can't you use <?=$two?>
  • Chris Laplante
    Chris Laplante over 10 years
    @pqsk: That is correct - I guess I should add a note about that too. Thanks!
  • omikes
    omikes about 6 years
    I think even the name Blade is a not-so-subtle reference to Razor.
  • AminM
    AminM almost 6 years
    @what bout smarty?