public function Twig_Environment::addGlobal

Registers a Global.

New globals can be added before compiling or rendering a template; but after, you can only update existing globals.

Parameters

string $name The global name:

mixed $value The global value:

File

drupal/core/vendor/twig/twig/lib/Twig/Environment.php, line 996

Class

Twig_Environment
Stores the Twig configuration.

Code

public function addGlobal($name, $value) {
  if ($this->extensionInitialized || $this->runtimeInitialized) {
    if (null === $this->globals) {
      $this->globals = $this
        ->initGlobals();
    }

    /* This condition must be uncommented in Twig 2.0
       if (!array_key_exists($name, $this->globals)) {
           throw new LogicException(sprintf('Unable to add global "%s" as the runtime or the extensions have already been initialized.', $name));
       }
       */
  }
  if ($this->extensionInitialized || $this->runtimeInitialized) {

    // update the value
    $this->globals[$name] = $value;
  }
  else {
    $this->staging
      ->addGlobal($name, $value);
  }
}