diff --git a/src/Runtime/InheritanceRuntime.php b/src/Runtime/InheritanceRuntime.php index 74ea85440..60cf79803 100644 --- a/src/Runtime/InheritanceRuntime.php +++ b/src/Runtime/InheritanceRuntime.php @@ -69,8 +69,12 @@ class InheritanceRuntime { * @param array $blockNames outer level block name */ public function init(Template $tpl, $initChild, $blockNames = []) { - // if called while executing parent template it must be a sub-template with new inheritance root - if ($initChild && $this->state === 3 && (strpos($tpl->template_resource, 'extendsall') === false)) { + // if called while executing parent template it must be a sub-template with new inheritance root. + // A new root is started either by a child template ($initChild) or by a sub-template included + // outside of any block rendering (empty source stack); the latter must not inherit the leftover + // block overrides of a previously completed inheritance tree (see issue #1189). + if (($initChild || empty($this->sourceStack)) && $this->state === 3 + && (strpos($tpl->template_resource, 'extendsall') === false)) { $tpl->setInheritance(clone $tpl->getSmarty()->getRuntime('Inheritance')); $tpl->getInheritance()->init($tpl, $initChild, $blockNames); return; diff --git a/tests/UnitTests/TemplateSource/_Issues/1189/IncludeExtendsBlockLeakIssue1189Test.php b/tests/UnitTests/TemplateSource/_Issues/1189/IncludeExtendsBlockLeakIssue1189Test.php new file mode 100644 index 000000000..ca6178bcf --- /dev/null +++ b/tests/UnitTests/TemplateSource/_Issues/1189/IncludeExtendsBlockLeakIssue1189Test.php @@ -0,0 +1,33 @@ +setUpSmarty(__DIR__); + } + + /** + * Sequence: include parent -> include child(extends parent) -> include parent. + * Expected: PARENT CHILD PARENT + * Bug (#1189): PARENT CHILD CHILD + */ + public function testBlockOverrideDoesNotLeakIntoLaterParentInclude() + { + $result = $this->smarty->fetch('top.tpl'); + $this->assertSame('PARENT CHILD PARENT', preg_replace('/\s+/', ' ', trim($result))); + } +} diff --git a/tests/UnitTests/TemplateSource/_Issues/1189/templates/child.tpl b/tests/UnitTests/TemplateSource/_Issues/1189/templates/child.tpl new file mode 100644 index 000000000..7e4bc0169 --- /dev/null +++ b/tests/UnitTests/TemplateSource/_Issues/1189/templates/child.tpl @@ -0,0 +1,2 @@ +{extends file="parent.tpl"} +{block name=message}CHILD{/block} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/_Issues/1189/templates/parent.tpl b/tests/UnitTests/TemplateSource/_Issues/1189/templates/parent.tpl new file mode 100644 index 000000000..3729d1728 --- /dev/null +++ b/tests/UnitTests/TemplateSource/_Issues/1189/templates/parent.tpl @@ -0,0 +1 @@ +{block name=message}PARENT{/block} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/_Issues/1189/templates/top.tpl b/tests/UnitTests/TemplateSource/_Issues/1189/templates/top.tpl new file mode 100644 index 000000000..9033de37a --- /dev/null +++ b/tests/UnitTests/TemplateSource/_Issues/1189/templates/top.tpl @@ -0,0 +1 @@ +{include file="parent.tpl"} {include file="child.tpl"} {include file="parent.tpl"} \ No newline at end of file