This section explains the basics of how to implement and use a custom Twig extension.
Create the SiteExtention.php file following this folder structure:
YourNamespace/YourBundle/Twig/Extension/SiteExtension.php
Then define the namespace, and the class like the example below:
<?php
namespace YourNamespace\SiteBundle\Twig\Extension;
class SiteExtension extends \Twig_Extension
{
public function getName()
{
return 'site_extension';
}
public function getFilters()
{
return [
new \Twig_SimpleFilter('yourfiltername', [$this, 'YourFunctionName'], array('is_safe' => array('html'))),
];
}
public function YourFunctionName($parameter)
{
return [];
}
}
In src/YourNamespace/YourBundle/config/services.yml file, add those lines:
# TwigExtensions
YourNamespace.twig.site_extension:
class: YourNamespace\YourBundle\Twig\Extension\SiteExtension
tags:
- { name: twig.extension }
In your Twig template, use your custom filter like any other:
{{ 'some text'|yourfiltername }}
See more documentation here: Custom Twig Extension