将你的{texy}标签添加到Latte中的方法

91 阅读1分钟

Texy库从3.1.6版开始以标签的形式增加了对Latte 3的支持{texy} 。它的作用是什么,如何部署它?

{texy} 标签是一种直接用Texy语法编写Latte模板的简便方法。

{texy}
You Already Know the Syntax
----------

No kidding, you know Latte syntax already. **It is the same as PHP syntax.**
{/texy}

只需在Latte中安装扩展,并根据需要传递给它一个配置好的Texy对象。

$texy = new Texy\Texy;
$latte = new Latte\Engine;
$latte->addExtension(new Texy\Bridges\Latte\TexyExtension($texy));

如果在{texy}...{/texy} 标签之间有静态文本,它将在模板编译时被Texy翻译,结果将被保存在模板中。如果内容是动态的(即里面有Latte标签),每次渲染模板时都要进行Texy处理。

如果希望关闭里面的拿铁标签,可以这样做。

{texy syntax: off} ... {/texy}

除了Texy对象外,还可以向扩展传递一个自定义函数,以允许从模板中传递参数。例如,我们希望能够传递参数localeheading

$processor = function (string $text, int $heading = 1, string $locale = 'cs'): string {
	$texy = new Texy\Texy;
	$texy->headingModule->top = $heading;
	$texy->typographyModule->locale = $locale;
	return $texy->process($text);
};

$latte = new Latte\Engine;
$latte->addExtension(new Texy\Bridges\Latte\TexyExtension($processor));

在模板中传递参数,如下所示。

{texy locale: en, heading: 3}
...
{/texy}

如果你想用Texy来格式化存储在变量中的文本,你可以使用一个过滤器。

{$description|texy}