My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GettingStarted  
Introducing "Razor view renderer" - view templates renderer extension for Yii PHP framework.
yii, razor, view, renderer, php, extension
Updated Aug 26, 2010 by Stepan.K...@gmail.com

Overview

Razor does not require you to explicitly close the code-block.

Compact, Expressive, and Fluid: Razor minimizes the number of characters and keystrokes required in a file, and enables a fast, fluid coding workflow. Unlike most template syntaxes, you do not need to interrupt your coding to explicitly denote server blocks within your HTML. The parser is smart enough to infer this from your code. This enables a really compact and expressive syntax which is clean, fast and fun to type.

Easy to Learn: Razor is easy to learn and enables you to quickly be productive with a minimum of concepts. You use all your existing language and HTML skills.

Configuration

At first download last version of RazorViewRenderer.php. You can checkout it from here (svn checkout https://yii-razor-view-render.googlecode.com/svn/trunk/) Save RazorViewRenderer.php under protected/extensions/renderers folder.

To use RazorViewRenderer, configure it as an application component named "viewRenderer" in the application configuration:

array(
     'components'=>array(
         ......
         'viewRenderer'=>array(
             'class'=>'ext.renderers.RazorViewRenderer',
             'fileExtension'=>'.tpl',
         ),
     ),
)

Syntax

Hello World Sample with Razor

You denote the start of a code block with Razor using a @ character. Unlike <?php ?> code nuggets, Razor does not require you to explicitly close the code-block:

<h1>Hello world sample with Razor</h1>

<p>
   Hello @$name, Today is: @date("Y-m-d")
</p>

<div>
    <a href="@Yii::app()->params['baseUrl']/login">Login</a>
</div>

Email addresses

In cases where the content is valid as code as well (and you want to treat it as content), you can explicitly escape out @ characters by typing @@.

<p>My email is: stepan.krab@@domain.com</p>

HTML Encoding

Content emitted using a @: block is automatically HTML encoded to better protect against XSS attack scenarios.

Loops and Nested HTML Sample

Supported @foreach, @for and @while loop statements. Example:

<ul class="products">
    @foreach ($products as $p) {
        <li>@$p->getName() - $@$p->price</li>
    }
</ul>

Notice above how we started a foreach loop using the @ symbol, and then contained a line of HTML content with code blocks within it. Because the Razor parser understands the PHP in our code block, it was able to determine that the <li> content should be contained within the foreach and treated like content that should be looped. It also recognized that the trailing } terminated the foreach statement.

The ability to code like this without having to add lots of open/close markers throughout your templates ends up making the whole coding process really fluid and fast.

If-Else block statements

Like our foreach example above, you can embed content within if statements (or any other PHP language construct), without having to be explicit about the code block’s begin/end. For example:

@if (count($products) == 0) {
    <p>Sorry - no products in this category.</p>
} else {
   <div class="red">We have products for you!</div>
}

Supported syntax for statements: @if, @if...else, @if...elseif...elseif...else

Multi-line Statements

You can denote multiple lines of code by wrapping it within a @{ code } block like so:

@{
    $obj = new Obj('config');
    $someVar = $obj->getValue() * 2;
}

Multi-Token Statements

The @( ) syntax enables a code block to have multiple tokens. For example, we could re-write the above code to concatenate a string and the number together within a @( code ) block:

<p>Message is: @("Some text " . $number)</p>

or

<div class="main">Status: @($activeFlag ? "Active" : "Not active")</div>

Nested statements

There is valid razor template:

<h1>@$caption</h1>

@for($i=1; $i<=10; ++$i){
   <a href="@Html::someHelper(array('param1','param2'), 'test')">@$obj->getMethod()->getField</a>
   @if($i == 4){this is fourth line}
}

<p>
    Value of array's item: @$someArray['first_dim']['second_dim']<br/>
    Value of first_dim: @($someArray['first_dim'])['this will be treated as html']<br/>
</p>

Notice: Block {,} brackets for @for, @foreach, @while, @if statements are required.

TODO

Maybe some short aliases for widget and cache? Something like: @w(...), @w(...){...}, @c(...){...}

Comment by mcbo...@gmail.com, Feb 25, 2011

Is there a syntax highlighter available for Eclipse/Aptana?

Comment by project member Stepan.K...@gmail.com, Feb 26, 2011

Unfortunately no

Comment by nalz...@gmail.com, Mar 9, 2011

Great :) I come from asp.net mvc 3, just want to use Yii. I don't know it can extend the view renderer, Yii-Razor are possible. It should have built into the official core. Lets make it popular and someone may develop highlighter for Eclipse/Aptana/NetBean?. I am very tired with smarty like template.

Based on this wiki, I think maybe you miss the server side comment @ @.

Comment by nalz...@gmail.com, Mar 9, 2011

Google delete the * symbol. server side comment @* *@


Sign in to add a comment
Powered by Google Project Hosting