pagekit / razr
此软件包已被 弃用 且不再维护。没有建议的替代软件包。
Razr - 强大的PHP模板引擎
dev-master / 0.9.x-dev
2014-07-26 10:33 UTC
Requires
- php: >=5.3.3
This package is not auto-updated.
Last update: 2023-12-04 23:45:11 UTC
README
Razr 是一个强大的PHP模板引擎,其语法受到ASP.NET Razor的启发。
用法
渲染模板
$razr = new Razr\Engine(new Razr\Loader\StringLoader); echo $razr->render('Hello @( $name )!', array('name' => 'World'));
带缓存渲染模板文件
$razr = new Razr\Engine(new Razr\Loader\FilesystemLoader(__DIR__), '/path/to/cache'); echo $razr->render('hello.razr.php', array('name' => 'World'));
语法
Razr 语法使用 @
作为特殊字符。它用于指示模板引擎的动态语句。在 @()
标注中,您可以使用常规PHP。以下语句受到支持。
输出数据
使用 @()
标注来输出任何PHP数据,默认启用转义。
示例
<h1>@( $title )</h1> @( 23 * 42 ) @( "<Data> is escaped by default." )
输出
<h1>Some title</h1> 966 <Data> is escaped by default.
输出原始数据
使用 @raw()
指令输出任何PHP数据,不进行转义。
示例
@raw("This will <strong>not</strong> be escaped.")
输出
This will <strong>not</strong> be escaped.
变量
您可以使用以下点 .
标注来访问单个变量以及数组/对象中的嵌套变量。
array( 'title' => 'I am the walrus', 'artist' => array( 'name' => 'The Beatles', 'homepage' => 'http://www.thebeatles.com', ) )
示例
<h1>@( $title )</h1> <p>by @( $artist.name ), @( $artist.homepage )</p>
输出
<h1>I am the walrus</h1> <p>by The Beatles, http://www.thebeatles.com</p>
设置变量值
示例
@set($msg = "Hello World!") @( $msg )
输出
Hello World!
条件控制结构
使用 @if
、@elseif
、@else
进行条件控制结构。使用任何布尔PHP表达式。
示例
@set($expression = false) @if( $expression ) One. @elseif ( !$expression ) Two. @else Three. @endif
输出
Two.
循环
您可以使用循环语句,如 foreach
和 while
。
@foreach($values as $key => $value) <p>@( $key ) - @( $value )</p> @endforeach @foreach([1,2,3] as $number) <p>@( $number )</p> @endforeach @while(true) <p>Infinite loop.</p> @endwhile
包含
使用部分和 @include
指令将可重用的标记片段提取到外部文件。您可以作为第二个参数传递一个参数数组。
示例
<section>@include('partial.razr', ['param' => 'parameter'])</section>
partial.razr
:
<p>Partial with @( $param )<p>
输出
<section><p>Partial with parameter<p><section>
使用块扩展模板
使用 @block
指令在模板中定义块。其他模板文件可以扩展这些文件并为定义的块定义自己的内容,而无需更改其他标记。
示例
@include('child.razr', ['param' => 'parameter'])
parent.razr
:
<h1>Parent template</h1> @block('contentblock') <p>Parent content.</p> @endblock <p>Parent content outside of the block.</p>
child.razr
:
@extend('parent.razr') @block('contentblock') <p>You can extend themes and overwrite content inside blocks. Paremeters are available as well: @( $param ).</p> @endblock
输出
<h1>Parent template</h1> <p>You can extend themes and overwrite content inside blocks. Paremeters are available as well: parameter.</p> <p>Parent content outside of the block.</p>