Access to each variable is very cumbersome, so in the end I had to create an extension that contains the data I need:
class SampleExtension extends Twig_Extension { private $foo; function getName() { return 'sampleExtension'; } function getFunctions() { return array( 'setFoo' => new Twig_Function_Method($this, 'setFoo') ); } function setFoo($value) { $this->foo = $value; } function getFoo() { return $this->foo; } }
And in the class where I need the data:
$this->sampleExtension = new SampleExtension(); $twigEnv->addExtension($this->sampleExtension); ... $html = $twigEnv->render('myTemplate.tpt', ...);
Using this template:
... {{ setFoo('bar') }} ...
After rendering:
echo $this->sampleExtension->getFoo();
source share