class TwigEnvironment extends Environment (View source)

A class that defines a Twig environment for Drupal.

Instances of this class are used to store the configuration and extensions, and are used to load templates from the file system or other locations.

Constants

CACHE_PREFIX_METADATA_KEY

Key name of the Twig cache prefix metadata key-value pair in State.

Properties

protected StateInterface $state

The state service.

protected array $templateClasses

Static cache of template classes.

protected string $twigCachePrefix

The template cache filename prefix.

Methods

__construct(string $root, CacheBackendInterface $cache, string $twig_extension_hash, StateInterface $state, LoaderInterface $loader, array $options = [])

Constructs a TwigEnvironment object and stores cache and storage internally.

compileSource(Source $source)

{@inheritdoc}

invalidate()

Invalidates all compiled Twig templates.

string
getTwigCachePrefix()

Get the cache prefixed used by \Drupal\Core\Template\TwigPhpStorageCache.

array
getTemplateMetadata(string $name)

Retrieves metadata associated with a template.

string
getTemplateClass(string $name, int $index = NULL)

Gets the template class associated with the given string.

renderInline(string $template_string, array $context = [])

Renders a twig string directly.

Details

__construct(string $root, CacheBackendInterface $cache, string $twig_extension_hash, StateInterface $state, LoaderInterface $loader, array $options = [])

Constructs a TwigEnvironment object and stores cache and storage internally.

Parameters

string $root

The app root.

CacheBackendInterface $cache

The cache bin.

string $twig_extension_hash

The Twig extension hash.

StateInterface $state

The state service.

LoaderInterface $loader

The Twig loader or loader chain.

array $options

The options for the Twig environment.

compileSource(Source $source)

{@inheritdoc}

Parameters

Source $source

invalidate()

Invalidates all compiled Twig templates.

See also

drupal_flush_all_caches

string getTwigCachePrefix()

Get the cache prefixed used by \Drupal\Core\Template\TwigPhpStorageCache.

Return Value

string

The file cache prefix, or empty string if the cache is disabled.

array getTemplateMetadata(string $name)

Retrieves metadata associated with a template.

Parameters

string $name

The name for which to calculate the template class name.

Return Value

array

The template metadata, if any.

Exceptions

LoaderError
SyntaxError

string getTemplateClass(string $name, int $index = NULL)

Gets the template class associated with the given string.

Parameters

string $name

The name for which to calculate the template class name.

int $index

The index if it is an embedded template.

Return Value

string

The template class name.

MarkupInterface|string renderInline(string $template_string, array $context = [])

Renders a twig string directly.

Warning: You should use the render element 'inline_template' together with the #template attribute instead of this method directly. On top of that you have to ensure that the template string is not dynamic but just an ordinary static php string, because there may be installations using read-only PHPStorage that want to generate all possible twig templates as part of a build step. So it is important that an automated script can find the templates and extract them. This is only possible if the template is a regular string.

Parameters

string $template_string

The template string to render with placeholders.

array $context

An array of parameters to pass to the template.

Return Value

MarkupInterface|string

The rendered inline template as a Markup object.

See also

StringLoader::exists