Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor syntax PHP equivalent

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

like image 977
Miguel Avatar asked Sep 05 '10 13:09

Miguel


People also ask

Is Cshtml a razor?

cshtml files are razorpages or MVC views, they does not contain any C#-written client-side code. If you wan to do so, you must use JavaScript. However, a . razor file, also know as a Razor component, can have C# written in it and run on client's browser.

What is Razor syntax in ASP.NET MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.

What is razor in asp net?

What is Razor? Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. Server-based code can create dynamic web content on the fly, while a web page is written to the browser.


2 Answers

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!

like image 53
Sir CodesALot Avatar answered Sep 27 '22 23:09

Sir CodesALot


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.

like image 22
mindplay.dk Avatar answered Sep 28 '22 00:09

mindplay.dk