4 * This file is part of Twig.
6 * (c) 2012 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
11 class Twig_Extension_StringLoader
extends Twig_Extension
16 public function getFunctions()
19 new Twig_SimpleFunction('template_from_string', 'twig_template_from_string', array('needs_environment' => true)),
26 public function getName()
28 return 'string_loader';
33 * Loads a template from a string.
36 * {{ include(template_from_string("Hello {{ name }}")) }}
39 * @param Twig_Environment $env A Twig_Environment instance
40 * @param string $template A template as a string
42 * @return Twig_Template A Twig_Template instance
44 function twig_template_from_string(Twig_Environment
$env, $template)
48 if (null === $loader) {
49 $loader = new Twig_Loader_String();
52 $current = $env->getLoader();
53 $env->setLoader($loader);
55 $template = $env->loadTemplate($template);
56 } catch (Exception
$e) {
57 $env->setLoader($current);
61 $env->setLoader($current);