About Template Engine Twig

This module adds the Twig template engine to the TemplateEngineFactory

Category 1Development Tools
Modules that have more to do with assisting development of a site rather than managing its content.
Category 2Markup Generation
Markup modules that are called upon to generate or parse markup (like HTML). Markup modules are most often used on the front-end of a site (rather than admin).
Category 3Other Modules
Modules that have a unique purpose not covered by existing categories.
Release StateStable
Should be safe for use in production environments. *
Authorwanze
Module Version1.1.0
Class NameTemplateEngineTwig
Compatibility2.3, 2.4, 2.5, 2.6, 2.7, 3.0
Date AddedJuly 3, 2014
Last UpdatedFebruary 13, 2019
Recommended ByNew recommendations may take up to 1 day to appear.

Details

Version 2.x of this module cannot be installed from the modules directory! It must be installed with Composer, as Twig is no longer bundled with the module. Version 2.x is only compatible with 2.x of the TemplateEngineFactory.

Version 1.x is still available on the 1.x branch.

Instructions

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

Code:
composer require wanze/template-engine-twig:^2.0

1.x
Download the latest 1.x release from GitHub and place the folder "TemplateEngineTwig" in "/site/modules".

README

TemplateEngineTwig

Build Status StyleCI License: MIT ProcessWire 3

A ProcessWire module adding Twig 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 wanze/template-engine-twig:^2.0

This will install the TemplateEngineTwig and TemplateEngineFactory modules in one step. Afterwards, don't forgetto enable Twig as engine in the TemplateEngineFactory module's configuration.

ℹ️ This module includes test dependencies. If you are installing on production with composer install, make sure topass 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 twig.html.
  • Provide ProcessWire API variables in Twig templates API variables ($pages, $input, $config...)are accessible in Twig, e.g. {{ config }} for the config API variable.
  • Debug If enabled, Twig outputs debug information. The module also registers the Debug Extension, offeringthe {{ dump() }} function to inspect variables.
  • Auto reload templates (recompile) If enabled, templates are recompiled whenever the source code changes.
  • Strict variables If set to false, Twig will silently ignore invalid variables (variables andor attributes/methods that do not exist) and replace them with a null value. When set to true,Twig throws an exception instead
  • Auto escape variables If enabled, templates will auto-escape variables. If you are using ProcessWiretextformatters to escape field values, do not enable this feature.

Extending Twig

It is possible to extend Twig after it has been initialized by the module. Hook the method TemplateEngineTwig::initTwig 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('TemplateEngineTwig::initTwig', function (HookEvent $event) {
    /** @var \Twig_Environment $twig */
    $twig = $event->arguments('twig');
    $twig->addFunction(new \Twig_Function('processwire', function () {
        return 'ProcessWire rocks!';
    }));
});
// ... and then use it anywhere in a Twig template:
{{ processwire() }}

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.

Use Twig Extensions

The Twig Extensions is a library that provides several usefulextensions for Twig.

These extensions are not included in the module by default, but you can add them to Twig using the same hook explainedabove.

Fist, install the library with Composer in the ProcessWire root directory: composer require twig/extensions. Next,use the same hook as above to register the desired extensions.

wire()->addHookAfter('TemplateEngineTwig::initTwig', function (HookEvent $event) {
    /** @var \Twig_Environment $twig */
    $twig = $event->arguments('twig');
    
    // Register the extensions.
    $twig->addExtension(new Twig_Extensions_Extension_Text());
    $twig->addExtension(new Twig_Extensions_Extension_I18n());
    $twig->addExtension(new Twig_Extensions_Extension_Intl());
});

Comments

No comments yet. Be the first to post!

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.