What's on this page
Add a custom Twig function or filter
Cecil provides many template functions and filters, but you can add your own with a Twig extension written in PHP.
Create the extension class
Add a PHP file in the extensions directory of your site, following the Cecil\Renderer\Extension namespace:
extensions/Cecil/Renderer/Extension/MyTwigExtension.php
<?php
namespace Cecil\Renderer\Extension;
class MyTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getFilters()
{
return [
// {{ 'text'|md5 }}
new \Twig\TwigFilter('md5', 'md5'),
// {{ page.content|word_count }}
new \Twig\TwigFilter('word_count', [$this, 'wordCount']),
];
}
public function getFunctions()
{
return [
// {{ year() }}
new \Twig\TwigFunction('year', fn () => date('Y')),
];
}
public function wordCount(string $html): int
{
return str_word_count(strip_tags($html));
}
}
Register the extension
Add the class name to the layouts.extensions list in cecil.yml (the key is a free name):
layouts:
extensions:
MyExtension: Cecil\Renderer\Extension\MyTwigExtension
Use it in templates
<p>{{ page.content|word_count }} words</p>
<footer>© {{ year() }} {{ site.title }}</footer>