I'm trying to create a new twig filter. I'm confused by this tutorial: https://twig.symfony.com/doc/2.x/advanced.html#filters
I don't know where should I put the code (which file, which path)...
I created a file called SnipText.php
<?
$filter = new Twig_Filter('snip', function ($context, $texto) {
$textoArr = explode(" ", preg_replace('/<[^>]*>/', '', $texto));
$qtdWords = count($textoArr);
$texto = '';
for($i=0;$i<40;$i++){
if($qtdWords > $i)
$texto .= $textoArr[$i] . ' ';
}
$texto .= '...';
return $texto;
}, array('needs_context' => true));
$twig = new Twig_Environment($loader);
$twig->addFilter($filter);
?>
And then I put it on my src folder. Twig is in my vendor folder. I get the error:
Fatal error: Uncaught Twig_Error_Syntax: Unknown "snip" filter.
Can someone show me step by step on how to create a filter on twig
?
twig
and adding your filter to that instance rather than to the instance you are rendering your templates with. – DarkBee