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

Breadcrumb

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

function Environment::createTemplate

Creates a template from source.

This method should not be used as a generic way to load templates.

Parameters

string $template The template source:

string|null $name An optional name of the template to be used in error messages:

Throws

LoaderError When the template cannot be found

SyntaxError When an error occurred during compilation

File

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

Class

Environment
Stores the Twig configuration and renders templates.

Namespace

Twig

Code

public function createTemplate(string $template, ?string $name = null) : TemplateWrapper {
    $hash = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $template, false);
    if (null !== $name) {
        $name = \sprintf('%s (string template %s)', $name, $hash);
    }
    else {
        $name = \sprintf('__string_template__%s', $hash);
    }
    $loader = new ChainLoader([
        new ArrayLoader([
            $name => $template,
        ]),
        $current = $this->getLoader(),
    ]);
    $this->setLoader($loader);
    try {
        return new TemplateWrapper($this, $this->loadTemplate($this->getTemplateClass($name), $name));
    } finally {
        $this->setLoader($current);
    }
}
RSS feed
Powered by Drupal