cliqon/clqrazr

Razr - 强大的PHP模板引擎

dev-main 2023-05-03 10:10 UTC

This package is auto-updated.

Last update: 2024-10-03 13:10:36 UTC


README

Everest-Php Razr的副本,针对PHP 8.1进行了Cliqon更改

Razr - 强大的PHP模板引擎

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
&lt;Data&gt; 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.

循环

您可以使用循环语句,如foreachwhile

@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>