Using a custom function in twig

Use getFunctions() instead of getFilters()

public function getFunctions()
{
    return array(
        new \Twig_SimpleFunction('server_time_zone', array($this, 'getServerTimeZone')),
    );
}

Twig filters are used to filter some value.

{{ "some value" | filter_name_here }}

Btw, you can define both filters and functions in the same class.


Instead of getFilters, override getFunctions and use Twig_Function_Method instead of Twig_Filter_Method.


In the newer versions of Twig he should be using Twig_SimpleFunction instead of Twig_Function_Method and Twig_SimpleFilter instead of Twig_Filter_Method, since Twig_*_Method are deprecated (I am using Twig v. 1.24.0 along with Symfony 2.8.2)

Tags:

Php

Twig

Symfony