Skip to content

Commit

Permalink
Non-present sections should not throw an exception with the
Browse files Browse the repository at this point in the history
strict_variables option
  • Loading branch information
LeSuisse committed Sep 22, 2017
1 parent 6a5504e commit 5606384
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/Mustache/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
';

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mustache/Exception/UnknownVariableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ public function getVariableName()
{
return $this->variableName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public function testPrevious()
$e = new Mustache_Exception_UnknownVariableException('foo', $previous);
$this->assertSame($previous, $e->getPrevious());
}
}
}
38 changes: 38 additions & 0 deletions test/Mustache/Test/FiveThree/Functional/StrictVariablesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of Mustache.php.
*
* (c) 2017 Enalean
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* @group lambdas
* @group functional
*/
class Mustache_Test_FiveThree_Functional_StrictVariablesTest extends PHPUnit_Framework_TestCase
{
private $mustache;

public function setUp()
{
$this->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')));
}
}

0 comments on commit 5606384

Please sign in to comment.