Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Environment.php

function Environment::__construct

Constructor.

Available options:

  • debug: When set to true, it automatically set "auto_reload" to true as well (default to false).
  • charset: The charset used by the templates (default to UTF-8).
  • cache: An absolute path where to store the compiled templates, a \Twig\Cache\CacheInterface implementation, or false to disable compilation cache (default).
  • auto_reload: Whether to reload the template if the original source changed. If you don't provide the auto_reload option, it will be determined automatically based on the debug value.
  • strict_variables: Whether to ignore invalid variables in templates (default to false).
  • autoescape: Whether to enable auto-escaping (default to html):

    • false: disable auto-escaping
    • html, js: set the autoescaping to one of the supported strategies
    • name: set the autoescaping strategy based on the template name extension
    • PHP callback: a PHP callback that returns an escaping strategy based on the template "name"
  • optimizations: A flag that indicates which optimizations to apply (default to -1 which means that all optimizations are enabled; set it to 0 to disable).
  • use_yield: true: forces templates to exclusively use "yield" instead of "echo" (all extensions must be yield ready) false (default): allows templates to use a mix of "yield" and "echo" calls to allow for a progressive migration Switch to "true" when possible as this will be the only supported mode in Twig 4.0
2 calls to Environment::__construct()
TwigEnvironment::__construct in core/lib/Drupal/Core/Template/TwigEnvironment.php
Creates a TwigEnvironment object, including its cache and storage.
TwigEnvironment::__construct in core/lib/Drupal/Core/Template/TwigEnvironment.php
Creates a TwigEnvironment object, including its cache and storage.
1 method overrides Environment::__construct()
TwigEnvironment::__construct in core/lib/Drupal/Core/Template/TwigEnvironment.php
Creates a TwigEnvironment object, including its cache and storage.

File

vendor/twig/twig/src/Environment.php, line 112

Class

Environment
Stores the Twig configuration and renders templates.

Namespace

Twig

Code

public function __construct(LoaderInterface $loader, array $options = []) {
    $this->setLoader($loader);
    $options = array_merge([
        'debug' => false,
        'charset' => 'UTF-8',
        'strict_variables' => false,
        'autoescape' => 'html',
        'cache' => false,
        'auto_reload' => null,
        'optimizations' => -1,
        'use_yield' => false,
    ], $options);
    $this->useYield = (bool) $options['use_yield'];
    $this->debug = (bool) $options['debug'];
    $this->setCharset($options['charset'] ?? 'UTF-8');
    $this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
    $this->strictVariables = (bool) $options['strict_variables'];
    $this->setCache($options['cache']);
    $this->extensionSet = new ExtensionSet();
    $this->defaultRuntimeLoader = new FactoryRuntimeLoader([
        EscaperRuntime::class => function () {
            return new EscaperRuntime($this->charset);
        },
    ]);
    $this->addExtension(new CoreExtension());
    $escaperExt = new EscaperExtension($options['autoescape']);
    $escaperExt->setEnvironment($this, false);
    $this->addExtension($escaperExt);
    if (\PHP_VERSION_ID >= 80000) {
        $this->addExtension(new YieldNotReadyExtension($this->useYield));
    }
    $this->addExtension(new OptimizerExtension($options['optimizations']));
}
RSS feed
Powered by Drupal