Download the PHP package heimrichhannot/contao-twig-support-bundle without Composer
On this page you can find all versions of the php package heimrichhannot/contao-twig-support-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download heimrichhannot/contao-twig-support-bundle
More information about heimrichhannot/contao-twig-support-bundle
Files in heimrichhannot/contao-twig-support-bundle
Package contao-twig-support-bundle
Short Description This bundle add twig template support to contao
License LGPL-3.0-or-later
Informations about the package contao-twig-support-bundle
Contao Twig Support Bundle
A bundle to enable extensive twig templates support in contao. Just activate the template loader and you can work with twig templates as they were .html5 templates. This means, you can select twig templates in the custom template select in the contao backend and you can override any template in your project template folder or in a bundle. As a developer, you can use this bundle to work with twig templates like with contao templates, e.g. use simple template names, template groups and rely on the contao template hierarchy.
Features
- enable twig support in contao
- use twig to write contao templates
- select twig templates in your module/element custom template selection or override default templates in twig
- support for all types of templates including widgets
- use this bundle as library to work with twig templates in your own bundles with extensive template loader
- uses caching for fast performance (is disabled in dev mode)
- minimal dependencies ;)
Usage
Setup
Install via contao manager or composer:
composer require heimrichhannot/contao-twig-support-bundle
Your first template
-
If you want to use this bundle to enable contao to support twig templates, you need to enable the template loader. You just need to add following configuration entries to your config.yml:
- Now you can create every contao template as twig template. For example create a twig template
ce_text_custom.html.twig
and add it to your projekttemplates
folder (in contao 4.4:app/Resources/views
). Now you can select the template as custom Template in the text content element (maybe you need to clear the cache before). You can override every core template that is parsed byparseTemplate
andparseWidget
hook. You can also add templates from bundles (just place them insrc/Resources/views
).
Widgets
As already written, this bundle also allows overriding widget templates. Since twig templates are not rendered in the default widget/template scope as contao templates, you can't use $this
to get variables. Instead, twig support bundle passes the widget instance, so you can use the widget object to get the variable content, for example {{ widget.name }}
or {{ widget.class }}
.
Twig and contao cavets
User input is encoded by contao, so you need to add the raw filter to variables if you need to output html.
Use project templates folder for twig templates in contao 4.4
If you want to use the project templates folder also in contao 4.4, just add following lines to your config.yml:
Skip templates
It may the be case, that other bundle comes with twig templates that are incompatible with core template but have the same name. Or you don't want to process some templates as twig templates. In this case you can add them to the skip templates configuration:
There is also a SkipTemplateException
that could be thrown in the BeforeParseTwigTemplateEvent
to skip a template.
Developers
Events
Event | Description |
---|---|
BeforeParseTwigTemplateEvent | Dispatched after twig templates was found. Throw a SkipTemplateException to skip the curent template to be processed as twig template. |
BeforeRenderTwigTemplate | Dispatched before twig templates is rendered. |
Use twig in your bundle
This bundle is a great base if you want to use twig in your own bundles.
Template locator
Use the TwigTemplateLocator
service to load your templates, where you need them while keeping the contao template hierarchy (you can override a bundle template in your project template folder or in another bundle which is loaded after the bundle).
Get all Templates with prefix (like Controller::getTemplateGroup): TwigTemplateLocator::getTemplateGroup()
Get the twig path from an template name:
Template renderer
Use the TwigTemplateRenderer
service to render a template by template name. The renderer adds template comments in dev mode as contao does for html5 templates. There are additional config options to customize the renderer or render a specific twig template instead of using the template locator to get the correct path to an template name. If you need to add specific logic applied before or after rendering, we recommend to decorate the service.
TwigFrontendTemplate
You can use the TwigFrontendTemplate
class to work with a twig template as it's a normal contao frontend template object. It inherits from the contao FrontendTemplate class and can be used to render twig template in contao context and use all hooks and template class functions.
Configuration reference
Credits
- thanks to m-vo and his Twig Bundle which implementation was in inspiration and inital basis for this bundle.
All versions of contao-twig-support-bundle with dependencies
contao/core-bundle Version ^4.13
ext-json Version *
symfony/cache Version ^4.4||^5.4
symfony/config Version ^4.4||^5.4
symfony/event-dispatcher Version ^4.4||^5.4
symfony/event-dispatcher-contracts Version ^1||^2||^3
symfony/finder Version ^4.4||^5.4
symfony/http-foundation Version ^4.4||^5.4
symfony/http-kernel Version ^4.4||^5.4
symfony/stopwatch Version ^4.4||^5.4
twig/twig Version ^1.26||^2.7||^3.0
webmozart/path-util Version ^2.3