blob: 7b4514710073c78a8cf6c0caf3009dcc6faac2f4 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?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 {
13use Twig\TwigFunction;
14
15final 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
26namespace {
27use Twig\Environment;
28use 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 */
38function twig_template_from_string(Environment $env, $template, string $name = null): TemplateWrapper
39{
40 return $env->createTemplate((string) $template, $name);
41}
42}