twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.2k stars 1.26k forks source link

Globals by reference #2691

Closed stdex closed 6 years ago

stdex commented 6 years ago

Is there any possibility to add global variable by reference?

stof commented 6 years ago

nope. Twig does not support setting Twig variables by reference (trying to do so may provide unexpected results, and will likely not work).

stdex commented 6 years ago

In 1.x versions it was possible to tweak Twig_Environment class, add something like this:

public function addGlobalRef($name, &$value)
{
    $this->staging['globals'][$name] = &$value;
    $this->globals = null;
}
stof commented 6 years ago

@stdex I'm almost sure that such globals would not stay references until the end when reaching Twig templates, as the global variables are copied into the context. So even if you thought it worked in 1.x, I'm not convinced at all that it was actually working.

lyrixx commented 6 years ago

@stdex you can use an object where you set / unset some variable in it. This is what we do in symfony

fabpot commented 6 years ago

Closing as this is not something we will support in Twig.

EliuFlorez commented 4 years ago

Variable global no working version 2.12.3 for what?