--- /dev/null
+``template_from_string``
+========================
+
+.. versionadded:: 1.11
+ The template_from_string function was added in Twig 1.11.
+
+The ``template_from_string`` function loads a template from a string:
+
+.. code-block:: jinja
+
+ {% include template_from_string("Hello {{ name }}") }}
+ {% include template_from_string(page.template) }}
+
+.. note::
+
+ The ``template_from_string`` function is not available by default. You
+ must add the ``Twig_Extension_StringLoader`` extension explicitly when
+ creating your Twig environment::
+
+ $twig = new Twig_Environment(...);
+ $twig->addExtension(new Twig_Extension_StringLoader());
+
+.. note::
+
+ Even if you will probably always use the ``template_from_string`` function
+ with the ``include`` tag, you can use it with any tag or function that
+ takes a template as an argument (like the ``embed`` or ``extends`` tags).
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2012 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_Extension_StringLoader extends Twig_Extension
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function getFunctions()
+ {
+ return array(
+ 'template_from_string' => new Twig_Function_Function('twig_template_from_string', array('needs_environment' => true)),
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getName()
+ {
+ return 'string_loader';
+ }
+}
+
+/**
+ * Loads a template from a string.
+ *
+ * <pre>
+ * {% include template_from_string("Hello {{ name }}") }}
+ * </pre>
+ *
+ * @param Twig_Environment $env A Twig_Environment instance
+ * @param string $template A template as a string
+ *
+ * @return Twig_Template A Twig_Template instance
+ */
+function twig_template_from_string(Twig_Environment $env, $template)
+{
+ static $loader;
+
+ if (null === $loader) {
+ $loader = new Twig_Loader_String();
+ }
+
+ $current = $env->getLoader();
+ $env->setLoader($loader);
+ try {
+ $template = $env->loadTemplate($template);
+ } catch (Exception $e) {
+ $env->setLoader($current);
+
+ throw $e;
+ }
+ $env->setLoader($current);
+
+ return $template;
+}
--- /dev/null
+--TEST--
+"template_from_string" function
+--TEMPLATE--
+{% include template_from_string(template) %}
+
+{% include template_from_string("Hello {{ name }}") %}
+--DATA--
+return array('name' => 'Fabien', 'template' => "Hello {{ name }}")
+--EXPECT--
+Hello Fabien
+Hello Fabien