twig tutorial - Creating a Twig Extension - twig php - twig template



Creating a Twig_Extension :

  • You can group all your custom functions/filters/tests/... inside a custom Twig_Extension class:
learn twig - twig tutorial - twig components - twig code -   twig meaning  - twig programming - twig download - twig examples
learn twig - twig tutorial - twig components - twig code -
twig meaning
- twig programming - twig download - twig examples

ProjectTwigExtension

class ProjectTwigExtension extends Twig_Extension {

    public function getFunctions() {
        return array(
            new Twig_SimpleFunction('twig_function_name', array($this, 'getTwigFunctionName')),
            new Twig_SimpleFunction('twig_function_foo', array($this, 'getTwigFunctionFoo')),            
        );



    }
    public function getFilters() {
        return array(
            new Twig_SimpleFilter('twig_filter_name' , array($this, 'getTwigFilterName')),
            new Twig_SimpleFilter('twig_filter_foo' , array($this, 'getTwigFilterFoo')),
        );
    }
    
    public function getName() {
        return 'ProjectTwigExtension';
    }        
}

Register extension in twig

$twig = new Twig_Environment($loader);
$twig->addExtension(new ProjectTwigExtension());
learn twig - twig tutorial - twig components - twig code -   twig template - twig programming - twig download - twig examples
learn twig - twig tutorial - twig components - twig code -
twig template
- twig programming - twig download - twig examples

Related Searches to twig tutorial - Creating a Twig Extension