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