Created
July 29, 2014 11:37
-
-
Save tobalsan/d6f313ef91914b7256da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# src/Acme/HelloBundle/Twig/AcmeExtension.php | |
namespace Acme\HelloBundle\Twig; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\Locale\Locale; | |
class AcmeExtension extends \Twig_Extension | |
{ | |
protected $container; | |
public function __construct(ContainerInterface $container) | |
{ | |
$this->container = $container; | |
} | |
public function getGlobals() | |
{ | |
// Either just return the container available in templates, so to get parameters: {{ container.getParameter('param') }} | |
return array( | |
'container' => $this->container, | |
); | |
// OR make every parameter a twig global variable | |
$params = $this->container->getParameterBag()->all(); | |
$params['container'] = $this->container; | |
return $params; | |
} | |
public function getName() | |
{ | |
return 'acme_extension'; | |
} | |
} | |
# src/Acme/HelloBundle/Resources/config/services.yml | |
services: | |
acme.twig.acme_extension: | |
class: Acme\HelloBundle\Twig\AcmeExtension | |
arguments: [@service_container] | |
tags: | |
- { name: twig.extension } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment