Currently I place my function in a class and pass an instance of this class into template and call my required function as a class method.
{{ unneededclass.blah(
Full answer: http://twig.sensiolabs.org/doc/advanced.html#id2
I prefer to use Twig Extension like this:
namespace Some\Twig\Extensions;
class MenuExtensions extends \Twig_Extension
{
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('sidebar_menu', [$this, 'generate_sidebar_menu']),
);
}
public function generate_sidebar_menu($menu){
return $menu;
}
public function getName()
{
return 'menu';
}
}
In template:
{{ sidebar_menu('some text') }}