Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 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 | |
| 12 | namespace Twig\Extension { |
| 13 | use Twig\TwigFunction; |
| 14 | |
| 15 | final class StringLoaderExtension extends AbstractExtension |
| 16 | { |
| 17 | public function getFunctions(): array |
| 18 | { |
| 19 | return [ |
| 20 | new TwigFunction('template_from_string', 'twig_template_from_string', ['needs_environment' => true]), |
| 21 | ]; |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | namespace { |
| 27 | use Twig\Environment; |
| 28 | use Twig\TemplateWrapper; |
| 29 | |
| 30 | /** |
| 31 | * Loads a template from a string. |
| 32 | * |
| 33 | * {{ include(template_from_string("Hello {{ name }}")) }} |
| 34 | * |
| 35 | * @param string $template A template as a string or object implementing __toString() |
| 36 | * @param string $name An optional name of the template to be used in error messages |
| 37 | */ |
| 38 | function twig_template_from_string(Environment $env, $template, string $name = null): TemplateWrapper |
| 39 | { |
| 40 | return $env->createTemplate((string) $template, $name); |
| 41 | } |
| 42 | } |