Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``template_from_string`` |
| 2 | ======================== |
| 3 | |
| 4 | The ``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 | |
| 11 | To ease debugging, you can also give the template a name that will be part of |
| 12 | any 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 | |
| 33 | Arguments |
| 34 | --------- |
| 35 | |
| 36 | * ``template``: The template |
| 37 | * ``name``: A name for the template |