webiny /?template-engine
Webiny 模板引擎组件
Requires
- php: ^7
- smarty/smarty: 3.1.*
- webiny/config: ~1.6
- webiny/service-manager: ~1.6
- webiny/std-lib: ~1.6
Requires (Dev)
- mybuilder/phpunit-accelerator: dev-master
- phpunit/phpunit: ~6
README
TemplateEngine
组件提供了一个渲染视图模板的层。
安装组件
安装组件的最佳方式是使用Composer。
composer require webiny/template-engine
要查看包的附加版本,请访问Packagist页面。
安装错误
如果您遇到如下错误
[RuntimeException]
Package could not be downloaded, sh: 1: svn: not found
则可能是因为您没有安装Subversion,这阻止了Smarty库的安装。这个问题可以通过在composer安装之前安装Subversion来解决。
$ sudo apt-get install subversion
使用方法
视图模板的定义取决于所选的驱动程序。默认情况下,模板引擎包含一个用于Smarty
的驱动程序,但您也可以轻松添加对Twig
或其他模板引擎的支持。
每个驱动程序提供的功能由TemplateEngineInterface
定义,该接口定义了以下方法
- fetch - 从指定位置获取模板,解析它并返回输出
- render - 从指定位置获取模板,解析它并将结果输出到浏览器
- assign - 将变量及其值分配给模板引擎
- setTemplateDir - 存储模板文件的目录
- registerPlugin - 为模板引擎注册插件
要创建一个新的驱动程序,只需创建一个新的类,该类实现了\Webiny\Component\TemplateEngine\Bridge\TemplateEngineInterface
,并适应您的配置。
默认配置如下
TemplateEngine: Engines: Smarty: ForceCompile: false CacheDir: '/var/tmp/smarty/cache' CompileDir: '/var/tmp/smarty/compile' TemplateDir: '/var/www/theme/templates' AutoEscapeOutput: false MuteExpectedErrors: true
使用方法
建议使用TemplateEngineTrait
。以下是一个示例
class MyClass { use \Webiny\Component\TemplateEngine\TemplateEngineTrait; function __construct() { // assing name and id to the template and render it $this->templateEngine('Smarty')->render('template.tpl', ['name'=>'John', 'id'=>15]); } }
Smarty
如果您希望使用内置驱动程序的Smarty模板引擎,请确保在组件使用之前,在应用程序的某个位置包含path/to/Smarty/libs/Smarty.class.php
。这是由于Smarty没有提供适合与组件集成的自动加载器。
插件和扩展
模板引擎设计得可以扩展为不同的插件和修饰符,具体取决于分配的驱动程序。
扩展模板引擎的最佳实践是首先创建一个扩展,然后将其注册为标记为$driverName.Extension
的服务,例如Smarty.Extension
。
一个Extension
是一个包含一个或多个插件的包。插件类型取决于模板引擎,例如,Smarty支持以下插件类型
- 函数 - https://smarty.php.ac.cn/docs/en/plugins.functions.tpl
- 修饰符 - https://smarty.php.ac.cn/docs/en/plugins.modifiers.tpl
- 块 - https://smarty.php.ac.cn/docs/en/plugins.block.functions.tpl
- 编译器函数 - https://smarty.php.ac.cn/docs/en/plugins.compiler.functions.tpl
- 预过滤器 - https://smarty.php.ac.cn/docs/en/plugins.prefilters.postfilters.tpl
- 后过滤器 - https://smarty.php.ac.cn/docs/en/plugins.prefilters.postfilters.tpl
- 输出过滤器 - https://smarty.php.ac.cn/docs/en/plugins.outputfilters.tpl
- 资源 - https://smarty.php.ac.cn/docs/en/plugins.resources.tpl
- 插入 - https://smarty.php.ac.cn/docs/en/plugins.inserts.tpl
要创建一个smarty扩展,创建一个继承自\Webiny\Component\TemplateEngine\Drivers\Smarty\AbstractSmartyExtension
的类,然后根据你想要创建的插件类型覆盖方法。
例如,假设我们想要注册一个名为'customUpper'的修饰符。首先,我们创建我们的扩展类如下
namespace MyApp\Demo; class MySmartyExtension extends \Webiny\Component\TemplateEngine\Drivers\Smarty\SmartyExtension { /** * @overwrite * @return array */ function getModifiers(){ return [ new SmartySimplePlugin('custom_upper', 'modifier', [$this, 'customUpper']) ]; } /** * Callback for my custom_upper modifier. * * @param $params * * @return string */ function customUpper($params){ return strtoupper($params); } /** * Returns the name of the plugin. * * @return string */ function getName() { return 'my_extension'; } }
一旦我们有了扩展,我们必须使用服务管理器来注册它
MyApp: CustomExtension: Class: \MyApp\Demo\MySmartyExtension Tags: [Smarty.Extension]
就这样,我们现在可以在模板中使用这个修饰符了
{'this is my name'|custom_upper} // outputs: THIS IS MY NAME
资源
要运行单元测试,您需要使用以下命令
$ cd path/to/Webiny/Component/TemplateEngine/
$ composer.phar install
$ phpunit