From 56063842fd40b31be98e3e2f1523142c843a1504 Mon Sep 17 00:00:00 2001 From: Thomas Gerbet Date: Fri, 22 Sep 2017 23:10:03 +0200 Subject: [PATCH] Non-present sections should not throw an exception with the strict_variables option --- src/Mustache/Compiler.php | 12 +++++- .../Exception/UnknownVariableException.php | 2 +- .../UnknownVariableExceptionTest.php | 2 +- .../Functional/StrictVariablesTest.php | 38 +++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 test/Mustache/Test/FiveThree/Functional/StrictVariablesTest.php diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index 5d2f79e1..734fbb6b 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -328,7 +328,11 @@ private function block($nodes) const SECTION_CALL = ' // %s section - $value = $context->%s(%s);%s + try { + $value = $context->%s(%s);%s + } catch (Mustache_Exception_UnknownVariableException $ex) { + $value = ""; + } $buffer .= $this->section%s($context, $indent, $value); '; @@ -403,7 +407,11 @@ private function section($nodes, $id, $filters, $start, $end, $otag, $ctag, $lev const INVERTED_SECTION = ' // %s inverted section - $value = $context->%s(%s);%s + try { + $value = $context->%s(%s);%s + } catch (Mustache_Exception_UnknownVariableException $ex) { + $value = ""; + } if (empty($value)) { %s } diff --git a/src/Mustache/Exception/UnknownVariableException.php b/src/Mustache/Exception/UnknownVariableException.php index 35ad9008..7851ec62 100644 --- a/src/Mustache/Exception/UnknownVariableException.php +++ b/src/Mustache/Exception/UnknownVariableException.php @@ -38,4 +38,4 @@ public function getVariableName() { return $this->variableName; } -} \ No newline at end of file +} diff --git a/test/Mustache/Test/Exception/UnknownVariableExceptionTest.php b/test/Mustache/Test/Exception/UnknownVariableExceptionTest.php index 83e4a77e..246a4ff6 100644 --- a/test/Mustache/Test/Exception/UnknownVariableExceptionTest.php +++ b/test/Mustache/Test/Exception/UnknownVariableExceptionTest.php @@ -41,4 +41,4 @@ public function testPrevious() $e = new Mustache_Exception_UnknownVariableException('foo', $previous); $this->assertSame($previous, $e->getPrevious()); } -} \ No newline at end of file +} diff --git a/test/Mustache/Test/FiveThree/Functional/StrictVariablesTest.php b/test/Mustache/Test/FiveThree/Functional/StrictVariablesTest.php new file mode 100644 index 00000000..0a14b706 --- /dev/null +++ b/test/Mustache/Test/FiveThree/Functional/StrictVariablesTest.php @@ -0,0 +1,38 @@ +mustache = new Mustache_Engine(array('strict_variables' => true)); + } + + public function testStrictVariablesInSection() + { + $tpl = $this->mustache->loadTemplate('{{#wrapper}}{{name}}{{/wrapper}}{{foo}}'); + + $this->assertEquals('bar', $tpl->render(array('foo' => 'bar'))); + } + + public function testStrictVariablesInInvertedSections() + { + $tpl = $this->mustache->loadTemplate('{{^wrapper}}{{foo}}{{/wrapper}}'); + + $this->assertEquals('bar', $tpl->render(array('foo' => 'bar'))); + } +}