blue-tomato / template-engine-smarty
ProcessWire 模块,将 Smarty 添加到 TemplateEngineFactory
2.6.0
2023-01-18 07:14 UTC
Requires
- php: >=7.0
- composer/installers: >=1.0
- smarty/smarty: >=4.1
README
A ProcessWire 模块,将 Smarty 添加到 TemplateEngineFactory。
要求
- ProcessWire
3.0
或更高版本 - TemplateEngineFactory
2.0
或更高版本 - PHP
7.0
或更高版本 - Composer
此模块的
1.x
版本可在 1.x 分支 上找到。如果您仍在使用 TemplateEngineFactory1.x
,请使用此版本。
安装
在您的 ProcessWire 安装根目录中执行以下命令
composer require blue-tomato/template-engine-smarty:^2.0
这将一次性安装 TemplateEngineSmarty 和 TemplateEngineFactory 模块。之后,不要忘记在 TemplateEngineFactory 模块的配置中启用 Smarty 作为引擎。
ℹ️ 此模块包含测试依赖项。如果您使用
composer install
在生产环境中安装,请确保传递--no-dev
标志以省略自动加载任何不必要的测试依赖项!
配置
模块提供了以下配置
模板文件后缀
Smarty 模板文件的后缀,默认为tpl
。在 Smarty 模板中提供 ProcessWire API 变量
API 变量(如$pages
、$input
、$config
...)在 Smarty 中可访问,例如,使用{{ config }}
访问配置 API 变量。调试
如果启用,Smarty 将输出调试信息。编译检查
如果启用,每次源代码更改时都会重新编译模板。错误报告
如果设置为false
,Smarty 将静默忽略无效变量(不存在变量或属性/方法)并将它们替换为null
值。如果设置为true
,Smarty 将引发异常。转义 HTML
如果启用,模板将自动转义变量。如果您使用 ProcessWire textformatters 转义字段值,请勿启用此功能。
扩展 Smarty
可以在模块初始化 Smarty 之后扩展 Smarty。通过将方法 TemplateEngineSmarty::initSmarty
钩子注册自定义函数、扩展、全局变量、过滤器等。
以下是一个如何使用提供的钩子附加自定义函数的示例。
function foo_function($params, $smarty) { return 'bar'; }; wire()->addHookAfter('TemplateEngineSmarty::initSmarty', function (HookEvent $event) { /** @var \Smarty $smarty */ $smarty = $event->arguments('smarty'); $smarty->registerPlugin("function", "foo", "foo_function"); }); // ... and then use it anywhere in a Smarty template: {foo}
上述钩子可以放在您的
site/init.php
文件中。如果您更喜欢使用模块,请将其放入模块的init()
方法中,并确保模块已自动加载。