Istediğiniz her türlü servis parametresini veya herhangi bir değeri twig extensionuna enjekte edebilirsiniz. Aşağıdaki örnek herhangi bir anlam ifade etmeyebilir ama faydalı bilgi verir.


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


Service container'in tamamının @service_container classa enjekte edilmesi "kötü pratik" olarak kabul edilir, bu nedenle bu işlem tavsiye edilmez.


# 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


Varsayalım ki name değişkeni "inanzzz" değerini tutacak şekilde, default controller içinde tanımlanmış olsun.


{{ name|print }}

Test


Eğer tarayıcınızdan http://sport.local/app_dev.php linkini açarsanız, aşağıdaki çıktıyı alırsınız.


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