saeven / zfc-twig
Laminas module that provides a Twig rendering strategy and extensions to render actions or trigger events from your templates
Requires
- php: ^7.4 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- laminas/laminas-mvc: ^3.0
- laminas/laminas-mvc-plugin-flashmessenger: ^1.0
- laminas/laminas-servicemanager: ^3.0
- laminas/laminas-view: ^2.7
- psr/container: ^1.1.2
- twig/twig: ^3.3.4
Requires (Dev)
- laminas/laminas-coding-standard: ^2.3.0
- laminas/laminas-modulemanager: ^2.7
- laminas/laminas-navigation: ^2.7
- phpunit/phpunit: ^9.5.9
Replaces
- zf-commons/zfc-twig: dev-master
This package is auto-updated.
Last update: 2024-10-20 04:40:41 UTC
README
ZfcTwig is a module that integrates the Twig templating engine with Laminas / Zend Framework.
Information
this is a fork of ZF-Commons/ZfcTwig. I added ZF3 support, so the module works with Laminas / Zend Framework 2 and 3. If you found a bug, please report it, just pm me in gitter or open a PullRequest.
Installation
- Add
"kokspflanze/zfc-twig": "dev-master"
to yourcomposer.json
file and runphp composer.phar update
. - Add
ZfcTwig
to yourconfig/application.config.php
file under themodules
key.
Configuration
ZfcTwig has sane defaults out of the box but offers optional configuration via the zfctwig
configuration key. For
detailed information on all available options see the module config file
class.
Documentation
Setting up Twig extensions
Extensions can be registered with Twig by adding the FQCN to the extensions
configuration key which is exactly how the
ZfcTwig extension is registered.
// in module configuration or autoload override return [ 'zfctwig' => [ 'extensions' => [ // an extension that uses no key 'My\Custom\Extension', // an extension with a key so that you can remove it from another module 'my_custom_extension' => 'My\Custom\Extension' ] ] ];
Configuring Twig loaders
By default, ZfcTwig
uses a Twig\Loader\ChainLoader
so that loaders can be chained together. A convenient default is setup using
a filesystem loader with the path set to
module/Application/view
which should work out of the box for most instances. If you wish to add additional loaders
to the chain you can register them by adding the service manager alias to the loaders
configuration key.
// in module configuration or autoload override return [ 'zfctwig' => [ 'loader_chain' => [ 'MyTwigFilesystemLoader' ] ] ]; // in some module public function getServiceConfiguration() { return [ 'factories' => [ 'MyTwigFilesystemLoader' => function($sm) { return new \Twig\Loader\FilesystemLoader('my/custom/twig/path'); } ] ]; }
Using ZF View Helpers
Using ZF view helpers is supported through the ZfcTwig\Twig\FallbackFunction function.
{# Simple view helper echo #} {{ doctype() }} {# Echo with additional methods #} {{ headTitle('My Company').setSeparator('-') }} {# Using a view helper without an echo #} {% do headTitle().setSeparator('-') %} {# Combining view helpers #} {% set url = ( url('my/custom/route') ) %}
Examples
Example .twig files for the skeleton application can be found in the examples folder.
Gotchas
ZF does not support multiple renderers with view helpers very well. As a workaround, ZfcTwig registers its own
HelperPluginManager
that extends the default Laminas\View\HelperPluginManager
and adds the default as a peering manager.
This let's ZfcTwig register its own renderer with view helpers that require it and fallback to the default manager for
view helpers that do not require one.
As a caveat, you must register view helpers that require a renderer with ZfcTwig. An example can be seen in
config/module.config.php
where the HelperConfig for the default navigation helpers is registered with ZfcTwig.