Skip to content
Browse files

Per the spec, missing partials should be treated as an empty string.

  • Loading branch information...
1 parent b7a6ff2 commit 99a5e9b14a93c62287c9867302fbbe4d939eb8a1 @bobthecow committed Mar 24, 2012
Showing with 21 additions and 2 deletions.
  1. +5 −1 src/Mustache/Compiler.php
  2. +5 −1 src/Mustache/Mustache.php
  3. +11 −0 test/Mustache/Test/MustacheTest.php
View
6 src/Mustache/Compiler.php
@@ -221,7 +221,11 @@ private function invertedSection($nodes, $id, $level) {
return sprintf($this->prepare(self::INVERTED_SECTION, $level), $id, $method, $id, $this->walk($nodes, $level));
}
- const PARTIAL = '$buffer .= $this->mustache->loadPartial(%s)->renderInternal($context, %s);';
+ const PARTIAL = '
+ if ($partial = $this->mustache->loadPartial(%s)) {
+ $buffer .= $partial->renderInternal($context, %s);
+ }
+ ';
/**
* Generate Mustache Template partial call PHP source.
View
6 src/Mustache/Mustache.php
@@ -414,7 +414,11 @@ public function loadTemplate($name) {
* @return Mustache_Template
*/
public function loadPartial($name) {
- return $this->loadSource($this->getPartialsLoader()->load($name));
+ try {
+ return $this->loadSource($this->getPartialsLoader()->load($name));
+ } catch (InvalidArgumentException $e) {
+ // If the named partial cannot be found, return null.
+ }
}
/**
View
11 test/Mustache/Test/MustacheTest.php
@@ -146,6 +146,17 @@ public function testImmutablePartialsLoadersThrowException() {
$mustache->setPartials(array('foo' => '{{ foo }}'));
}
+ public function testMissingPartialsTreatedAsEmptyString() {
+ $mustache = new Mustache_Mustache(array(
+ 'partials_loader' => new Mustache_Loader_ArrayLoader(array(
+ 'foo' => 'FOO',
+ 'baz' => 'BAZ',
+ ))
+ ));
+
+ $this->assertEquals('FOOBAZ', $mustache->render('{{>foo}}{{>bar}}{{>baz}}', array()));
+ }
+
public function testHelpers() {
$foo = function() { return 'foo'; };
$bar = 'BAR';

0 comments on commit 99a5e9b

Please sign in to comment.
Something went wrong with that request. Please try again.