Back to Top

Twig – How to create custom extensions or filters

Updated 5 years ago

In PHP template engine, Twig there are various inbuilt awesome filters using that anyone can write large apps in less no. of lines like

{% for user in users %}
//write code based on user
{% else %}
//you can add else condition in for array which will work is users is blank
{% endfor %}

and various others filter too but these are limited and various default PHP function are not available in Twig then how can we use those ? For those cases Twig provide ways so that you can add your custom filters/ extension. I am going to show you how.

First  – Create your custom extension class extending class \Twig_Extension 

<?php 
namespace Webkul\DemoBundle\Twig;

class DemoExtension extends \Twig_Extension
{
  //here we will write code
  //two functions are necessary
  // 1st - getFilters() where you declare all your custom extensions
  // 2nd - getName() for your Extension class name
}

It’s basic requirement is to declare two functions getFilters() and getName()  so we add those here. In getFilters() function we declare our all custom filters/ extensions and in getName() function we add this class name (any name).

<?php
namespace Webkul\DemoBundle\Twig;

class DemoExtension extends \Twig_Extension
{
    /**
     * Here we declare our custom filter with their respective function
     */
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('addslashes', array($this, 'addslashes')),
            new \Twig_SimpleFilter('utf8_decode', array($this, 'utf8_decode')),
            new \Twig_SimpleFilter('json_decode', array($this, 'json_decode')),
            new \Twig_SimpleFilter('urldecode', array($this, 'urldecode')),
            //here first urldecode is name which we use in twig and 2nd one is with refer to function written in this class
        );
    }

    /**
     * add slash on string
     */
    public function addslashes($string)
    {
        return addslashes($string);
    }

    /**
     * utf8 decode string
     */
    public function utf8_decode($string) {
        return utf8_decode($string);
    }
    
    /**
     * json decode string
     */
    public function json_decode($string) {
        return json_decode($string);
    }
    
    /**
     * urldecode string
     */
    public function urldecode($string) {
        return urldecode($string);
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'twig_extension';
    }
}

?>

So we created a class with various custom functions now we have to just notify Symfony about these filters/ extensions. We do it after declaring this in our service.

services:
    webkul.twig_extension:
        class: Webkul\DemoBundle\Twig\DemoExtension
        tags:
            - { name: twig.extension }

That’s it our Custom filters/ extensions are ready to use. We can use these like Twig default once.

{{ "Webkul's Awesome Symfony Twig Blog"|addslashes }}

Framework used – Symfony 2.7

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home