From 09241edd52bab2ac6b78c21919f060c680aa4e8e Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Thu, 29 Jun 2017 00:13:59 -0700 Subject: [PATCH] Use a local $blocksContext for each parent render. ... rather than a shared one per template / section / block that accidentally keeps old block context around. Fixes #322 --- src/Mustache/Compiler.php | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index dd40d8a..34c3958 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -191,7 +191,6 @@ class Mustache_Compiler { $this->lambdaHelper = new Mustache_LambdaHelper($this->mustache, $context); $buffer = \'\'; - $blocksContext = array(); %s return $buffer; @@ -207,7 +206,6 @@ class Mustache_Compiler public function renderInternal(Mustache_Context $context, $indent = \'\') { $buffer = \'\'; - $blocksContext = array(); %s return $buffer; @@ -270,7 +268,7 @@ class Mustache_Compiler return sprintf($this->prepare(self::BLOCK_VAR, $level), $id, $else); } - const BLOCK_ARG = '$blocksContext[%s] = array($this, \'block%s\');'; + const BLOCK_ARG = '%s => array($this, \'block%s\'),'; /** * Generate Mustache Template inheritance block argument PHP source. @@ -291,14 +289,13 @@ class Mustache_Compiler $keystr = var_export($key, true); $id = var_export($id, true); - return sprintf($this->prepare(self::BLOCK_ARG, 1), $id, $key); + return sprintf($this->prepare(self::BLOCK_ARG, $level), $id, $key); } const BLOCK_FUNCTION = ' public function block%s($context) { - $indent = $buffer = \'\'; - $blocksContext = array();%s + $indent = $buffer = \'\';%s return $buffer; } @@ -333,7 +330,6 @@ class Mustache_Compiler private function section%s(Mustache_Context $context, $indent, $value) { $buffer = \'\'; - $blocksContext = array(); if (%s) { $source = %s; @@ -463,15 +459,20 @@ class Mustache_Compiler } const PARENT = ' - %s - if ($parent = $this->mustache->loadPartial(%s)) { - $context->pushBlockContext($blocksContext); + $context->pushBlockContext(array(%s + )); $buffer .= $parent->renderInternal($context, $indent); $context->popBlockContext(); } '; + const PARENT_NO_CONTEXT = ' + if ($parent = $this->mustache->loadPartial(%s)) { + $buffer .= $parent->renderInternal($context, $indent); + } + '; + /** * Generate Mustache Template inheritance parent call PHP source. * @@ -486,11 +487,14 @@ class Mustache_Compiler { $realChildren = array_filter($children, array(__CLASS__, 'onlyBlockArgs')); + if (empty($realChildren)) { + return sprintf($this->prepare(self::PARENT_NO_CONTEXT, $level), var_export($id, true)); + } + return sprintf( $this->prepare(self::PARENT, $level), - $this->walk($realChildren, $level), var_export($id, true), - var_export($indent, true) + $this->walk($realChildren, $level + 1) ); }