keepkimi
keepkimi

Reputation: 393

How to prevent Symfony 2 from setting global variables in templates?

How to tell Symfony2 not to set global variables (like app) in templates? I'd like to set my own app variable but it has Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables Object type event if I overwrite it in my controller.

Upvotes: 2

Views: 530

Answers (1)

l3l0
l3l0

Reputation: 3393

I think that is such possibility (basically never did it cause app var can be handy ;) )

app global is added in constructor of Symfony\Bundle\TwigBundle\TwigEngine (for twig) and in constructor of Symfony\Bundle\FrameworkBundle\Templating\PhpEngine (for php)

but when you do not pass GlobalVariables to constructor (can be null) it will not set app variable. So you can overwrite templating service like that:

    <service id="templating" class="Acme\DemoBundle\Templating\TwigEngine">
        <argument type="service" id="twig" />
        <argument type="service" id="templating.name_parser" />
        <argument type="service" id="templating.locator" /> 
    </service>

and php file:

<?php 

namespace Acme\DemoBundle\Templating;

use Symfony\Bundle\TwigBundle\TwigEngine as BaseEngine;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Config\FileLocatorInterface;

class TwigEngine extends BaseEngine
{
    public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, FileLocatorInterface $locator)
    { 
        parent::__construct($environment, $parser, $locator);
    } 
}

You should be aware that you can implement own globals without that as well... just define own templating.globals service which will replace original one.

Upvotes: 1

Related Questions