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.

You can pass service parameters to twig extension as well as any other available parameters or values. Example below might not make sense but it is purely for demonstration purposes.


Parameters


# sport/app/config/parameters.yml
parameters:
project_name: Sport

Encrypt 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(
'encrypt',
array($this, 'encryptFilter')
)
);
}

public function encryptFilter($value)
{
return sha1($value);
}
}

Print Extension Class


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

use Doctrine\ORM\EntityManager;
use Symfony\Component\Routing\RouterInterface;
use Twig_Extension;
use Twig_SimpleFilter;

class PrintExtension extends Twig_Extension
{
private $entityManager;
private $router;
private $encryptExtension;
private $projectName;
private $randomValue;

public function __construct(
EntityManager $entityManager,
RouterInterface $router,
EncryptExtension $encryptExtension,
$projectName,
$randomValue
) {
$this->entityManager = $entityManager;
$this->router = $router;
$this->encryptExtension = $encryptExtension;
$this->projectName = $projectName;
$this->randomValue = $randomValue;
}

public function getName()
{
return 'print_extension';
}

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

public function printFilter($value)
{
return sprintf(
'Doctrine EM class: %s # Home URL: %s # Plain value: %s # Encrypted value: %s # Project: %s # Random: %s',
get_class($this->entityManager),
$this->router->generate('football_frontend_homepage'),
$value,
$this->encryptExtension->encryptFilter($value),
$this->projectName,
$this->randomValue
);
}
}

Config


Passing whole service container @service_container is considered as "bad practise" so don't do it.


# sport/src/Football/FrontendBundle/Resources/config/services.yml
services:

football_frontend.twig.extension.encrypt:
class: Football\FrontendBundle\Twig\EncryptExtension
public: false
tags:
- { name: twig.extension }

football_frontend.twig.extension.print:
class: Football\FrontendBundle\Twig\PrintExtension
public: false
arguments: [ @doctrine.orm.entity_manager, @router, @football_frontend.twig.extension.encrypt, %project_name%, 'London' ]
tags:
- { name: twig.extension }

Twig


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


{{ name|print }}

Test


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


Doctrine EM class: Doctrine\ORM\EntityManager # Home URL: /app_dev.php/ # Plain value: inanzzz # Encrypted value: bae9e624ee464e275fa804d047955bcc08caab5b # Project: Sport # Random: London