The Twig plugin renders content files using Twig templates.
This plugin requires the twig/twig
package. Install it with Composer:
composer require twig/twig
The Twig plugin constructor accepts two arguments: the path to the templates directory, and an optional Twig environment configuration.
$app->add(new Twig(
'./path/to/templates/`,
[
'cache' => false
]
));
The default template is page.twig
. You may choose a different template
for each content file by specifying the Twig template name in the file
header's template
property.
title = "My post"
template = "custom-post.twig"
---
<p>Post content goes here.</p>
Every Twig template has these variables:
page
- The current File instance.current_url
- The current page pathname with leading/
.site
- The payload object'ssite
property.
The page
Twig variable is a File instance, and therefore may be
used as an array to access its header properties and body content.
The site
Twig variable contains site-wide metadata and other tools provided by
previous plugins. For example, if you defined custom collections
before adding this Twig plugin, you can access those collections in your Twig
templates like this:
{% for pathname, file in site.collections.get("collection-name") %}
<a href="{{ pathname }}">{{ file.title }}</a>
{% endfor %}
You may also run custom queries at render-time using the collection
plugin's query()
method like this:
{% set files = site.collections.query({
where: { "author": ["Josh", "!="] },
sortBy: "date",
reverse: true,
limit: 5
}) %}
If you do NOT want a content file to be rendered with Twig, add a header property
to the appropriate content file with name twig
and value false
:
title: "My post"
twig: false
---
<p>This post will not use Twig.</p>