Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

We're going to encrypt a value with a custom twig extension in the example below. Encryption algorithm will be passed as an argument in extension.

Extension Class

# sport/src/Football/FrontendBundle/Twig/EncryptExtension.php
namespace Football\FrontendBundle\Twig;

use Twig_Extension;
use Twig_SimpleFilter;

class EncryptExtension extends Twig_Extension
public function getName()
return 'encrypt_extension';

public function getFilters()
return array(
new Twig_SimpleFilter(
array($this, 'encryptFilter')

public function encryptFilter($value, $algorithm = 'sha1')
return $algorithm($value);


# sport/src/Football/FrontendBundle/Resources/config/services.yml
class: Football\FrontendBundle\Twig\EncryptExtension
public: false
- { name: twig.extension }


Assuming that name variable is set in default controller and carries "inanzzz" as its value.

Hello {{ name }}!
<br />
Hello {{ name|encrypt }}!
<br />
Hello {{ name|encrypt('sha1') }}!
<br />
Hello {{ name|encrypt('md5') }}!


If you call http://sport.local/app_dev.php in your browser, you should see output below.

Hello inanzzz! 
Hello bae9e624ee464e275fa804d047955bcc08caab5b!
Hello bae9e624ee464e275fa804d047955bcc08caab5b!
Hello 144c8dcb89dc293f55c68cc74adda88b!