getting image extension in Twig Symfony2

symfony, twig

Solution

You can create Twig extension, that will contain

namespace YourApp\AcmeBundle\Twig;

class MyTwigExtension extends \Twig_Extension
{

public function getFilters(){
    return array(
        new \Twig_SimpleFilter('ext', array($this, 'ext')),
    );
}

public function ext($filepath){
    $ext = pathinfo($filepath, PATHINFO_EXTENSION);
    return $ext;
}

}

Problem

Am not very familiar with twig, am trying to get an image extention, but am not sure how to do this in twig ,in php it's very easy using string functions such as substr and indexof or with the following: ext=pathinfo('/testdir/dir2/image.gif', PATHINFO_EXTENSION), i don't want to code it in controller and pass it to twig as parameter,instead i want to extract it directly in the twig layout,so how am going to do this?

Original source