Razr - The powerful PHP template engine
Razr is a powerful PHP template engine for PHP, whose syntax was inspired by ASP.NET Razor., (*1)
Usage
Render a template:, (*2)
$razr = new Razr\Engine(new Razr\Loader\StringLoader);
echo $razr->render('Hello @( $name )!', array('name' => 'World'));
Render a template file with caching:, (*3)
$razr = new Razr\Engine(new Razr\Loader\FilesystemLoader(__DIR__), '/path/to/cache');
echo $razr->render('hello.razr.php', array('name' => 'World'));
Syntax
The Razr syntax uses @
as special character. It is used to indicate a dynamic statement for the template engine. Within the @()
notation you may use regular PHP. The following statements are supported., (*4)
Echo data
Use the @()
notation to echo any PHP data with escaping enabled by default., (*5)
Example, (*6)
<h1>@( $title )</h1>
@( 23 * 42 )
@( "<Data> is escaped by default." )
Output, (*7)
<h1>Some title</h1>
966
<Data> is escaped by default.
Echo raw data
Use the @raw()
directive to output any PHP data without escaping., (*8)
Example, (*9)
@raw("This will <strong>not</strong> be escaped.")
Output, (*10)
This will <strong>not</strong> be escaped.
Variables
You can access single variables and nested variables in arrays/objects using the following dot .
notation., (*11)
array(
'title' => 'I am the walrus',
'artist' => array(
'name' => 'The Beatles',
'homepage' => 'http://www.thebeatles.com',
)
)
Example, (*12)
<h1>@( $title )</h1>
<p>by @( $artist.name ), @( $artist.homepage )</p>
Output, (*13)
<h1>I am the walrus</h1>
<p>by The Beatles, http://www.thebeatles.com</p>
Set variable values
Example, (*14)
@set($msg = "Hello World!")
@( $msg )
Output, (*15)
Hello World!
Conditional control structures
Use @if
, @elseif
, @else
for conditional control structures. Use any boolean PHP expression., (*16)
Example, (*17)
@set($expression = false)
@if( $expression )
One.
@elseif ( !$expression )
Two.
@else
Three.
@endif
Output, (*18)
Two.
Loops
You can use loop statements like foreach
and while
., (*19)
@foreach($values as $key => $value)
@( $key ) - @( $value ), (*20)
@endforeach
@foreach([1,2,3] as $number)
@( $number ), (*21)
@endforeach
@while(true)
Infinite loop., (*22)
@endwhile
Include
Extract reusable pieces of markup to an external file using partials and the @include
directive. You can pass an array of arguments as a second parameter., (*23)
Example, (*24)
<section>@include('partial.razr', ['param' => 'parameter'])</section>
partial.razr
:, (*25)
<p>Partial with @( $param )<p>
Output, (*26)
<section><p>Partial with parameter<p><section>
Extending templates with blocks
Use the @block
directive to define blocks inside a template. Other template files can extend those files and define their own content for the defined blocks without changing the rest of the markup., (*27)
Example, (*28)
@include('child.razr', ['param' => 'parameter'])
parent.razr
:, (*29)
Parent template
@block('contentblock')
Parent content., (*30)
@endblock
Parent content outside of the block., (*31)
child.razr
:, (*32)
@extend('parent.razr')
@block('contentblock')
You can extend themes and overwrite content inside blocks. Paremeters are available as well: @( $param )., (*33)
@endblock
Output, (*34)
Parent template
You can extend themes and overwrite content inside blocks. Paremeters are available as well: parameter., (*35)
Parent content outside of the block., (*36)