I don't think the render function needs to be overwritten at all to be honest. I can think of a couple of ways that you could get access to some kind of global variable depending on how the global variable is set (or calculated).
Twig Globals
If it is a hardcoded variable then you could add it into your config.yml as one of the twig globals, like so...
# app/config/config.yml
twig:
# ...
globals:
ga_tracking: UA-xxxxx-x
Which can then be used like...
<p>The google tracking code is: {{ ga_tracking }}</p>
As explained here - http://symfony.com/doc/current/cookbook/templating/global_variables.html
Twig Function
Alternatively if you variable is related in some way to an outside service you could use a custom twig function and call that in you template.
The twig extension (with function)..
class AcmeExtension extends \Twig_Extension
{
protected $container;
public function __construct(ContainerInterface $container = null)
{
$this->container = $container;
}
public function getFunctions()
{
return array(
new \Twig_SimpleFilter(
'get_parameter',
array($this, 'getParameter')
),
);
}
public function getParameter($parameter)
{
if (null === $this->container) {
return null;
}
return $this->container->getParameter($parameter);
// You could also do perform some kind of action
// rather than just return a value
}
public function getName()
{
return 'acme_extension';
}
}
Registered as a service..
services:
acme.twig.acme_extension:
class: Acme\DemoBundle\Twig\AcmeExtension
arguments:
- @service_container
tags:
- { name: twig.extension }
And then in your template you could use..
{{ get_parameter('myVar') }}
Or even..
{% set myVar = get_parameter('myVar') %}