wireframe-framework / wireframe-renderer-latte
Wireframe输出框架的Latte渲染器。
Requires
- php: >=7.2
- latte/latte: ^2.0
- wireframe-framework/processwire-composer-installer: ^1.0.0
This package is auto-updated.
Last update: 2024-09-21 11:06:18 UTC
README
此模块是Wireframe输出框架的可选渲染器附加组件,增加了对Latte模板引擎的支持。
注意:此模块目前被视为早期beta版本。如果您遇到任何问题,请在此处提交问题https://github.com/wireframe-framework/WireframeRendererLatte/issues。
安装模块
此模块可以像其他ProcessWire模块一样安装,通过克隆或下载模块文件到您的模块目录。或者您也可以通过Composer安装它
composer require wireframe-framework/wireframe-renderer-latte
基本用法
首先,您需要安装Wireframe和WireframeRenderLatte,然后根据https://wireframe-framework.com/getting-started/中的说明设置Wireframe。完成这些后,您可以打开bootstrap文件(wireframe.php),并指示Wireframe使用Latte渲染器
// during Wireframe init (this is the preferred way): $wireframe->init([ 'renderer' => ['WireframeRendererLatte', [ 'latte' => [ // just an example (this is the default value) 'tempDirectory' => $this->wire('config')->paths->cache . '/WireframeRendererLatte', ], 'ext' => 'latte', // file extension ('latte' is the default value) ]], ]); // ... or after init (this incurs a slight overhead): $wireframe->setRenderer('WireframeRendererLatte', [ // optional settings array ]);
Latte模板
告诉Wireframe使用Latte渲染器后,默认情况下,它将尝试使用Latte渲染所有视图、布局和组件。Latte模板的文件扩展名为.latte
,尽管您可以选择其他扩展名(参见“基本用法”部分的示例)。
请注意,如果找不到Latte文件,Wireframe将自动回退到本地(.php
)文件。这是为了简化从PHP迁移到Latte,同时也使得Latte和PHP视图文件可以共存。
如果您需要关于Latte及其语法的帮助,请访问https://latte.nette.org/。
包含(部分)
Latte提供了一个包含其他模板的功能({include 'some.latte'}
),在Wireframe的上下文中,这最好对应于部分的概念。因此,使用此函数将在Wireframe部分目录中查找包含文件
{include 'header.latte'}
.
|-- partials
| `-- header.latte
扩展Latte
如果您想向Latte添加过滤器、函数、标签等,您可以通过挂钩到WireframeRendererLatte::initLatte
来访问Latte引擎。
// site/ready.php $wire->addHookAfter('WireframeRendererLatte::initLatte', function(HookEvent $event) { $event->return->addFilter('shortify', function (string $s): string { return mb_substr($s, 0, 10); // shortens the text to 10 characters }); });
{$page->name|shortify}