merged branch fabpot/complex-template_from_string (PR #1154)

This PR was merged into the master branch.

Discussion
----------

fixed template_from_string when the template includes or extends other ones

This should fix #1134

Commits
-------

1b5bf91 fixed template_from_string when the template includes or extends other ones
This commit is contained in:
Fabien Potencier
2013-08-04 15:01:42 +02:00
3 changed files with 10 additions and 7 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.14.0 (2013-XX-XX)
* fixed template_from_string when the template includes or extends other ones
* improved the performance of the filesystem loader
* fixed a crash of the C extension on an edge case
* fixed the filesystem loader cache when a template name exists in several namespaces
+5 -7
View File
@@ -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);
@@ -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