views:

304

answers:

5

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

+1  A: 

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

SimpleCoder
Your first example is missing a `;` after the string assignment.
Russell Dias
@Russell Dias - Thank you, I didn't spot that!
SimpleCoder
+1  A: 

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}

stillstanding
A: 

No, but my makrell engine is much better than that. Seriously. ;) especially how layouts/master pages are handled.

stereofrog
+1  A: 

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.

mbanzon
A: 

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.

mindplay.dk