Skip to content

Instantly share code, notes, and snippets.

@tobalsan
Created July 29, 2014 11:37
Show Gist options
  • Save tobalsan/d6f313ef91914b7256da to your computer and use it in GitHub Desktop.
Save tobalsan/d6f313ef91914b7256da to your computer and use it in GitHub Desktop.
<?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