TwigEnvironment::__construct

public TwigEnvironment::__construct($root, CacheBackendInterface $cache, $twig_extension_hash, \Twig_LoaderInterface $loader = NULL, $options = array())

Constructs a TwigEnvironment object and stores cache and storage internally.

Parameters

string $root: The app root.

\Drupal\Core\Cache\CacheBackendInterface $cache: The cache bin.

string $twig_extension_hash: The Twig extension hash.

\Twig_LoaderInterface $loader: The Twig loader or loader chain.

array $options: The options for the Twig environment.

Overrides Twig_Environment::__construct

File

core/lib/Drupal/Core/Template/TwigEnvironment.php, line 40

Class

TwigEnvironment
A class that defines a Twig environment for Drupal.

Namespace

Drupal\Core\Template

Code

public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, \Twig_LoaderInterface $loader = NULL, $options = array()) {
  // Ensure that twig.engine is loaded, given that it is needed to render a
  // template because functions like TwigExtension::escapeFilter() are called.
  require_once $root . '/core/themes/engines/twig/twig.engine';

  $this->templateClasses = array();

  $options += array(
    // @todo Ensure garbage collection of expired files.
    'cache' => TRUE,
    'debug' => FALSE,
    'auto_reload' => NULL,
  );
  // Ensure autoescaping is always on.
  $options['autoescape'] = 'html';

  $policy = new TwigSandboxPolicy();
  $sandbox = new \Twig_Extension_Sandbox($policy, TRUE);
  $this->addExtension($sandbox);

  if ($options['cache'] === TRUE) {
    $options['cache'] = new TwigPhpStorageCache($cache, $twig_extension_hash);
  }

  $this->loader = $loader;
  parent::__construct($this->loader, $options);
}
doc_Drupal
2016-10-29 09:49:37
Comments
Leave a Comment

Please login to continue.