diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 92874b4ab..d306964bd 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -20,6 +20,7 @@ abstract class Twig_Template implements Twig_TemplateInterface protected static $cache = array(); protected $parent; + protected $parents = array(); protected $env; protected $blocks; protected $traits; @@ -67,13 +68,26 @@ abstract class Twig_Template implements Twig_TemplateInterface try { $parent = $this->doGetParent($context); - return $this->parent = false === $parent ? false : $this->env->resolveTemplate($parent); + + if (false === $parent) { + return false; + } + + if ($parent instanceof Twig_Template) { + return $this->parents[$parent->getTemplateName()] = $parent; + } + + if (!isset($this->parents[$parent])) { + $this->parents[$parent] = $this->env->loadTemplate($parent); + } } catch (Twig_Error_Loader $e) { $e->setTemplateFile(null); $e->guess(); throw $e; } + + return $this->parents[$parent]; } protected function doGetParent(array $context) diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/multiple_dynamic.test b/test/Twig/Tests/Fixtures/tags/inheritance/multiple_dynamic.test new file mode 100644 index 000000000..1d3e639ca --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/inheritance/multiple_dynamic.test @@ -0,0 +1,22 @@ +--TEST-- +"extends" tag +--TEMPLATE-- +{% set foo = 1 %} +{{ include('parent.twig') }} +{{ include('parent.twig') }} +{% set foo = 2 %} +{{ include('parent.twig') }} +--TEMPLATE(parent.twig)-- +{% extends foo~'_parent.twig' %}{% block content %}{{ parent() }} parent{% endblock %} +--TEMPLATE(1_parent.twig)-- +{% block content %}1{% endblock %} +--TEMPLATE(2_parent.twig)-- +{% block content %}2{% endblock %} +--DATA-- +return array() +--EXPECT-- +1 parent + +1 parent + +2 parent