Is there an equivalent to the new ASP.NET razor syntax in PHP?
The only Razor-like syntax that PHP has something similar to is what I believe is called "variable expansion":
$two = "Two";
echo "One $two Three";
Result:
One Two Three
But, you can't use this outside of a PHP block or outside of a double-string for that matter (at least not for how you want to use it). This doesn't work:
<?php
$two = "Two";
?>
<p>One $two Three</p>
Result:
<p>One $two Three</p>
Other PHP programing constructs outside of PHP blocks do nothing as well. So, none of this stuff is paralleled in PHP: http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx
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}
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.
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.
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!
It seems someone did it: https://github.com/steffans/razr
But I would still convert to Twig, it is much more powerful.
Laravel's Blade Template Engine, uses a similar syntax to Razor. https://laravel.com/docs/master/blade
I'm actually working on a project that does exactly that! Just with a $
instead of a @
.
It's called Phazor for Node.
Just install it npm i phazor -g
<head>
/* Phazor comments inside html */
${
// Vanilla PHP inside ${ }
$title = "I love fruit";
$isFruit = true;
}
/* An echo expression */
<title>$(ucwords($title))</title>
</head>
<body>
/* Variable expression */
<h1>$title.</h1>
/* Inline statement */
$if ($isFruit) {
<p>It sure is great!</p>
}
</body>
Then run phazor sourceFolder destinationFolder
and it will compile all the phazor .ph files into the destination folder. (It can also compile TypeScript and SASS files inside the folder)
Keep in mind this is not fully tested in a production environment.