You are here

public function Twig_Environment::addGlobal in Translation template extractor 6.3

Same name and namespace in other branches
  1. 7.3 vendor/Twig/Environment.php \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

vendor/Twig/Environment.php, line 1026

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);
  }
}