How to make a twig extension in symfony2

Twig is a the flexible, fast, and secure PHP template engine.

I started  using twig since i found this in symfony2.

I was trying to use split function of php in twig but i was not able to split a text.
So i started to implement twig filter. In this tutorial I’m going to make a simple Twig extension.
First we have to write a twig extension.
Step 1: Create a Extension Folder in the Bundle
Step 2: Create a php file in Extension Folder called MyTwigExtension.php

Source Code
// DevDreamBundle/Extension/MyTwigExtension.php
namespace Dev\DreamBundle\Extension;

class MyTwigExtension extends \Twig_Extension {

    public function getFilters() {
        return array(
        'var_dump'   => new \Twig_Filter_Function('var_dump'),
       'highlight'  => new \Twig_Filter_Method($this, 'highlight'),
       'split'  => new \Twig_Filter_Method($this, 'split'),
       'countArray'  => new \Twig_Filter_Method($this, 'countArray'),

    public function highlight($sentence, $expr) 
        return preg_replace('/(' . $expr . ')/', 
 '<span style="color:red">\1</span>', $sentence);
    public function split($sentence, $expr) 
        return explode($expr, $sentence);
    public function countArray($sentence) 
        return count($sentence);

    public function getName()
        return 'my_twig_extension';



Now we have to make a dependency injection. So we have to  write a simple class that specifies what exactly our bundle needs, what services does it provide and eventualy configure our bundle.

Step 1: Create a DependencyInjection Folder in the Bundle

Step 2 : Make a php file called it hello Extension.php

Source Code

namespace Dev\DreamBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class HelloExtension extends Extension 
public function load(array $config, ContainerBuilder $container) 
 $definition = new Definition
 // this is the most important part. Later in the startup process TwigBundle
 // searches through the container and registres all services taged as twig.extension.
 $container->setDefinition('my_twig_extension', $definition);
 * Was necessary in previous Symfony2 PR releases.
 * Symfony2 calls `load` method automatically now.
 * public function getAlias() {
 * return 'hello'; // that's how we'll call this extension in configuration files
 * }

Now we have to just enable this twig. To do this

The Steps are :-

Open config.yml

# app/config/config.yml 
 class: Dev\DreamBundle\Extension\HelloExtension 
 - { name: twig.extension }

Now You are good to go

Its time to use this extension in Twig

{{ ‘Dev Dream  Live the life the way you want’|Split(‘ ‘)|raw }}


It will create a array and split every word.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s