Skip to content

Instantly share code, notes, and snippets.

@hidenorigoto
Created October 24, 2011 15:42
Show Gist options
  • Save hidenorigoto/1309341 to your computer and use it in GitHub Desktop.
Save hidenorigoto/1309341 to your computer and use it in GitHub Desktop.
Symfony\Bundle\TwigBundle\Extension\ContainerExtension example
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\Extension;
use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContainerExtension extends \Twig_Extension
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getFunctions()
{
return array(
'container_param' => new \Twig_Function_Method($this, 'getParameter'),
);
}
public function getParameter($key)
{
return $this->container->getParameter($key);
}
public function getName()
{
return 'container';
}
}
{{ container_param('kernel.root_dir') }}
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
:
<parameter key="twig.extension.container.class">Symfony\Bundle\TwigBundle\Extension\ContainerExtension</parameter>
:
</parameters>
<services>
:
<service id="twig.extension.container" class="%twig.extension.container.class%" public="false">
<tag name="twig.extension" />
<argument type="service" id="service_container" />
</service>
:
</services>
</container>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment