added a recipe about rendering a template stored in a string

This commit is contained in:
Fabien Potencier
2015-07-07 09:46:15 +02:00
parent d96a014e68
commit 4e58c4811a
+18
View File
@@ -421,4 +421,22 @@ logical name, and not the path from the filesystem::
Now that the ``base.twig`` templates is defined in an array loader, you can
remove it from the database, and everything else will still work as before.
Rendering a Template Stored in a String
---------------------------------------
The ``template_from_string`` Twig function allows a template stored in a string
to be rendered from another template:
.. code-block:: jinja
{{ include(template_from_string("Hello {{ name }}")) }}
Doing the same in PHP is also possible::
$twig = new \Twig_Environment(new \Twig_Loader_Array(array()));
$template = $twig->createTemplate('Hello {{ name }}');
$rendered = $template->render(array('name' => 'Bob'));
.. _callback: http://www.php.net/manual/en/function.is-callable.php