blue-tomato/template-engine-mustache

此包已废弃,不再维护。未建议替代包。

ProcessWire模块,向TemplateEngineFactory添加Mustache

2.0.0 2019-02-28 12:22 UTC

This package is auto-updated.

Last update: 2021-06-28 17:50:10 UTC


README

License: MIT ProcessWire 3

A ProcessWire模块,将Mustache添加到TemplateEngineFactory

要求

  • ProcessWire 3.0 或更高版本
  • TemplateEngineFactory 2.0 或更高版本
  • PHP 7.0 或更高版本
  • Composer

此模块的 1.x 版本可在 1.x 分支 上找到。如果您仍在使用 TemplateEngineFactory 1.x,请使用此版本。

安装

在ProcessWire安装根目录中执行以下命令

composer require blue-tomato/template-engine-mustache:^2.0

这将一步安装TemplateEngineMustacheTemplateEngineFactory模块。之后,别忘了在TemplateEngineFactory模块的配置中启用Mustache作为引擎。

ℹ️ 此模块包含测试依赖项。如果您使用composer install在生产环境中安装,请确保传递--no-dev标志以省略自动加载任何不必要的测试依赖项!。

配置

该模块提供以下配置

  • 模板文件后缀 Twig模板文件的后缀,默认为mustache
  • 在Mustache模板中提供ProcessWire API变量 API变量($pages$input$config...)在Twig中可访问,例如,使用配置API变量{{ config }}
  • 调试 如果启用,Mustache将输出调试信息。

扩展Mustache

可以在模块初始化Mustache之后扩展Mustache。通过注册自定义函数、扩展、全局变量、过滤器等,将方法TemplateEngineMustache::initMustache钩到。

以下是如何使用提供的钩子附加自定义函数的示例。

wire()->addHookAfter('TemplateEngineMustache::initMustache', function (HookEvent $event) {
    /** @var \Mustache_Engine $mustache */
    $mustache = $event->arguments('mustache');

    $mustache->setHelpers([
			'myHelperFunction' => function($text) {
				return trim($text);
			}
		]);
});

// ... and then use it anywhere in a Mustache template:

{{#myHelperFunction}} {{someVariable}} {{/myHelperFunction}}

上述钩子可以放在您的site/init.php文件中。如果您喜欢使用模块,将其放入模块的init()方法中,并确保模块已自动加载。