blob: 80e9d2dede07cc83e3f22b56dba3631f77639c5a [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001``template_from_string``
2========================
3
4The ``template_from_string`` function loads a template from a string:
5
6.. code-block:: twig
7
8 {{ include(template_from_string("Hello {{ name }}")) }}
9 {{ include(template_from_string(page.template)) }}
10
11To ease debugging, you can also give the template a name that will be part of
12any related error message:
13
14.. code-block:: twig
15
16 {{ include(template_from_string(page.template, "template for page " ~ page.name)) }}
17
18.. note::
19
20 The ``template_from_string`` function is not available by default. You
21 must add the ``\Twig\Extension\StringLoaderExtension`` extension explicitly when
22 creating your Twig environment::
23
24 $twig = new \Twig\Environment(...);
25 $twig->addExtension(new \Twig\Extension\StringLoaderExtension());
26
27.. note::
28
29 Even if you will probably always use the ``template_from_string`` function
30 with the ``include`` function, you can use it with any tag or function that
31 takes a template as an argument (like the ``embed`` or ``extends`` tags).
32
33Arguments
34---------
35
36* ``template``: The template
37* ``name``: A name for the template