Twig Plugin for Kirby CMS
- Adds support for Twig templates to Kirby CMS.
- Requires Kirby 2.3 (in beta as of 2016-04-12).
I don’t like Twig!
It’s okay, using this plugin is of course optional. You can keep using PHP templates in Kirby CMS.
I like Twig, but I don’t want to convert all my templates
Good news: you don’t have to! By default your PHP templates will keep working.
This plugin can work in two modes:
- Default: allow both Twig and PHP templates. (If both
mytemplate.twig
andmytemplate.php
exist, the Twig template takes precedence.) - Optional: allow Twig templates only.
Installation
You will need Composer, a command-line tool, to install this plugin.
- Download a copy of this repository and put it in your
site/plugins
folder. Rename the copied folder totwig
. - Open a terminal in your
site/plugins/twig
folder and runcomposer install
. - To activate the plugin, put
c::set('plugin.twig.enabled', true);
in yourconfig.php
.
You can now create .twig
templates in your site/templates
directory.
Options
// REQUIRED: activate Twig plugin
c::set('plugin.twig.enabled', true);
// Option: use Twig’s PHP cache in addition to Kirby’s HTML cache.
// (Only works when Kirby’s own cache is active.)
// Defaults to false
c::set('plugin.twig.cache', false);
// Option: disable or specify autoescaping type.
// http://twig.sensiolabs.org/doc/api.html#environment-options
// Defaults to true
c::set('plugin.twig.autoescape', true);
// Should we use .php templates as fallback when .twig
// templates don't exist? Set to false to only allow Twig templates.
// Defaults to true
c::set('plugin.twig.usephp', true);
Using Twig
See Twig templating tips for Kirby.
Known limitations
-
Only a subset of Kirby’s functions and helpers are exposed to Twig templates (to be documented).
-
Likewise, normal PHP functions are not available to Twig templates. If you want full PHP power, use PHP templates, or write Controllers that send data to your templates. Note that Twig already gives you a lot of tools for working with strings and arrays: Twig Reference.
-
By design, Twig will not let you include files from outside the
site/templates
directory. If you have a use care where this is a problem, please open an issue.