1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Twig\Extension; 13 14use Twig\Environment; 15use Twig\TemplateWrapper; 16use Twig\TwigFunction; 17 18final class StringLoaderExtension extends AbstractExtension 19{ 20 public function getFunctions(): array 21 { 22 return [ 23 new TwigFunction('template_from_string', [self::class, 'templateFromString'], ['needs_environment' => true]), 24 ]; 25 } 26 27 /** 28 * Loads a template from a string. 29 * 30 * {{ include(template_from_string("Hello {{ name }}")) }} 31 * 32 * Never expose `template_from_string` to untrusted template 33 * authors (like in a sandboxed environment). See the docs for more details. 34 * 35 * @param string|null $name An optional name of the template to be used in error messages 36 * 37 * @internal 38 */ 39 public static function templateFromString(Environment $env, string|\Stringable $template, ?string $name = null): TemplateWrapper 40 { 41 return $env->createTemplate((string) $template, $name); 42 } 43} 44