From 1b5bf91011c40b3ae40f389ee39d6c052d3fd718 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 3 Aug 2013 19:05:30 +0200 Subject: [PATCH] fixed template_from_string when the template includes or extends other ones --- CHANGELOG | 1 + lib/Twig/Extension/StringLoader.php | 12 +++++------- .../Fixtures/functions/template_from_string.test | 4 ++++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8be8ca6..b5b2074 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.14.0 (2013-XX-XX) + * fixed template_from_string when the template includes or extends other ones * added support for calling macros defined in a template without importing them first * added support for named arguments for macros * replaced a PHP fatal error with an exception when a macro does not exist diff --git a/lib/Twig/Extension/StringLoader.php b/lib/Twig/Extension/StringLoader.php index 20f3f99..a1cef87 100644 --- a/lib/Twig/Extension/StringLoader.php +++ b/lib/Twig/Extension/StringLoader.php @@ -43,16 +43,14 @@ class Twig_Extension_StringLoader extends Twig_Extension */ function twig_template_from_string(Twig_Environment $env, $template) { - static $loader; + $loader = new Twig_Loader_Chain(array( + new Twig_Loader_Array(array('__string_template__' => $template)), + $current = $env->getLoader(), + )); - if (null === $loader) { - $loader = new Twig_Loader_String(); - } - - $current = $env->getLoader(); $env->setLoader($loader); try { - $template = $env->loadTemplate($template); + $template = $env->loadTemplate('__string_template__'); } catch (Exception $e) { $env->setLoader($current); diff --git a/test/Twig/Tests/Fixtures/functions/template_from_string.test b/test/Twig/Tests/Fixtures/functions/template_from_string.test index 41428da..3d3b958 100644 --- a/test/Twig/Tests/Fixtures/functions/template_from_string.test +++ b/test/Twig/Tests/Fixtures/functions/template_from_string.test @@ -4,8 +4,12 @@ {% include template_from_string(template) %} {% include template_from_string("Hello {{ name }}") %} +{% include template_from_string('{% extends "parent.twig" %}{% block content %}Hello {{ name }}{% endblock %}') %} +--TEMPLATE(parent.twig)-- +{% block content %}{% endblock %} --DATA-- return array('name' => 'Fabien', 'template' => "Hello {{ name }}") --EXPECT-- Hello Fabien Hello Fabien +Hello Fabien -- 1.7.2.5