How to create a custom Twig function in Symfony 2

Hi all,

Did you ever wonder of creating your own custom twig function. For instance, assume that you are displaying set of url’s in your view (twig) file. Suddenly you want all or some of your url’s to be opened in a new tab once clicked. We all know this can easily be done by having the target attribute in anchor tag such as <a href = “link” target=”_blank”>Text</a>. This seems to be pretty straight forward. But if you want to add or remove this target attribute one day in all of your urls, it will be nightmare to change all down the line. Instead, if you have used a custom twig function to add this property to the link and use that function instead of html anchor tag in the twig file, things will be much easier when it comes to changing things later.

Let’s see how we do this,

  • To achieve what we wanted, we want to create a new class (which extends Twig_Extension). Create a new PHP class and save it in a your project. Define the class name extending ‘\Twig_Extension’. (Make sure you add the namespace)
  •  In the class file, you need to declare two functions. One is to register the custom function and one is to return the name of the class as a string. The first function you need to add is ‘getFilters()’ and register your custom function there.

public function getFilters()

{
return array(
new \Twig_SimpleFilter(‘custom_website’, array($this, ‘customWebsiteFilter’)),
);
}

If I describe what’s in the getFilter() function, it should return an array mentioning all the custom twig functions. As you already noticed, the label of our custom function (which we used to call from a twig file) is ‘custom_website’ and we pass an array which contains $this (current object / current calling object) and finally the real method name which we are going to declare in the same class in a minute. The corresponding method for label ‘custom_website‘ is ‘customWebsiteFilter()‘.

  • Before declare customWebsiteFilter() function you need to mention another function which is the ‘getName()’ to return a string. See the code below,

public function getName()
{
return ‘custom_extension’;
}

  • Now we must create the ‘customWebsiteFilter()’ function. See the function below,

public function customWebsiteFilter($website)

{
$http = substr($website, 0, 4);
if($http == ‘http’){
return ‘<a href=”‘.$website.'”       target=”_blank”>’.$website.'</a>’;
}else{
return ‘<a href=”http://’.$website.'”   target=”_blank”>’.$website.'</a>’;
}
}

In the customWebsiteFilter() function, as you see, we expect a parameter, which is the url of the website. Then inside the function we check whether it contains ‘http’ in the first four characters. If it does, we don’t amend the string with http. But if it doesn’t we prefix http at the beginning in href attribute. In both instances we have included ‘_blank’ as the value of target attribute. That’s why we wrote this function. Whenever someone use this twig function, the url will automatically be replaced with target=”_blank” attribute. You can see the summed up code below,

<?php

namespace ACME\CustomExtensionsBundle\Twig;

class CustomExtension extends \Twig_Extension
{

public function getFilters()

{
return array(
new \Twig_SimpleFilter(‘custom_website’, array($this, ‘customWebsiteFilter’)),
);
}

public function getName()

{
return ‘custom_extension’;
}

public function customWebsiteFilter($website)

{
$http = substr($website, 0, 4);
if($http == ‘http’){
return ‘<a href=”‘.$website.'” target=”_blank”>’.$website.'</a>’;
}else{
return ‘<a href=”http://’.$website.'” target=”_blank”>’.$website.'</a>’;
}
}

}

Finally you need to register this as a service in the config.yml file. See below,

custom_twig_extension:

class: Acme\CustomExtensionsBundle\Twig

tags:

– { name: twig.extension }

Now we are all set to use ‘custom_website’ twig function in any of the twig file. This is how you use it.

{{ “http://www.google.com” | custom_website | raw }}

This is how it works. We passed the string which contains “http://www.google.com” to “custom_website” label (which is the customWebsiteFilter function) which we created short while ago. As we discussed earlier this function adds the ” target=’_blank’ ” into it and returns it. The we use twig’s ‘raw‘ convert from a html function, otherwise it will show with href tags which we don’t want to show. That’s it. Like this function, you can pretty much do whatever you want. If you want to format a number in a custom way, this is the way you should follow.

I hope you learned something new today. Feel free to cast your feedback in the comments section below. Cheers! 🙂

Share

4 thoughts on “How to create a custom Twig function in Symfony 2

Leave a Reply

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