TemplateEngineMustache by blue-tomato

Mustache templates for the TemplateEngineFactory

TemplateEngineMustache

License: MIT ProcessWire 3

A ProcessWire module adding Mustache to the TemplateEngineFactory.

Requirements


  • ProcessWire 3.0 or newer
  • TemplateEngineFactory 2.0 or newer
  • PHP 7.0 or newer
  • Composer

The 1.x version of this module is available on the 1.x branch. Use this version if you still use TemplateEngineFactory 1.x.

Installation


Execute the following command in the root directory of your ProcessWire installation:

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

This will install the TemplateEngineMustache and TemplateEngineFactory modules in one step. Afterwards, don't forget to enable Mustache as engine in the TemplateEngineFactory module's configuration.

ℹ️ This module includes test dependencies. If you are installing on production with composer install, make sure to pass the --no-dev flag to omit autoloading any unnecessary test dependencies!.

Configuration


The module offers the following configuration:

  • Template files suffix The suffix of the Twig template files, defaults to mustache.
  • Provide ProcessWire API variables in Mustache templates API variables ($pages, $input, $config...) are accessible in Twig, e.g. {{ config }} for the config API variable.
  • Debug If enabled, Mustache outputs debug information.

Extending Mustache


It is possible to extend Mustache after it has been initialized by the module. Hook the method TemplateEngineMustache::initMustache to register custom functions, extensions, global variables, filters etc.

Here is an example how you can use the provided hook to attach a custom function.

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}}

The above hook can be put in your site/init.php file. If you prefer to use modules, put it into the module's init() method and make sure that the module is auto loaded.

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Latest news

  • ProcessWire Weekly #559
    The 559th issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 25 January 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“ProcessWire is like a breath of fresh air. So powerful yet simple to build with and customise, and web editors love it too.” —Margaret Chatwin, Web developer