Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move loader cascading into the loadPartial method.

This means the `partialsLoader` property is never set to the default loader, fixing things like `setPartials`, but template loading cascades as per the spec.

Add test coverage for partial cascading.
  • Loading branch information...
commit ca44ef68f8d2b232bf5c0ef4d598462d6ac92c98 1 parent 72007e6
Justin Hileman authored
Showing with 36 additions and 5 deletions.
  1. +12 −4 src/Mustache/Engine.php
  2. +24 −1 test/Mustache/Test/EngineTest.php
16 src/Mustache/Engine.php
View
@@ -226,7 +226,7 @@ public function setPartialsLoader(Mustache_Loader $partialsLoader)
public function getPartialsLoader()
{
if (!isset($this->partialsLoader)) {
- $this->partialsLoader = $this->getLoader();
+ $this->partialsLoader = new Mustache_Loader_ArrayLoader;
}
return $this->partialsLoader;
@@ -492,13 +492,21 @@ public function loadTemplate($name)
public function loadPartial($name)
{
try {
- return $this->loadSource($this->getPartialsLoader()->load($name));
- } catch (InvalidArgumentException $e) {
+ if (isset($this->partialsLoader)) {
+ $loader = $this->partialsLoader;
+ } elseif (isset($this->loader) && !$this->loader instanceof Mustache_Loader_StringLoader) {
+ $loader = $this->loader;
+ } else {
+ throw new Mustache_Exception_UnknownTemplateException($name);
+ }
+
+ return $this->loadSource($loader->load($name));
+ } catch (Mustache_Exception_UnknownTemplateException $e) {
// If the named partial cannot be found, log then return null.
$this->log(
Mustache_Logger::WARNING,
'Partial not found: "{name}"',
- array('name' => $name)
+ array('name' => $e->getTemplateName())
);
}
}
25 test/Mustache/Test/EngineTest.php
View
@@ -104,7 +104,7 @@ public function testSettingServices()
$mustache->setLoader($loader);
$this->assertSame($loader, $mustache->getLoader());
- $this->assertSame($loader, $mustache->getPartialsLoader());
+ $this->assertNotSame($loader, $mustache->getPartialsLoader());
$mustache->setPartialsLoader($loader);
$this->assertSame($loader, $mustache->getPartialsLoader());
@@ -240,6 +240,29 @@ public function testSetLoggerThrowsExceptions()
$mustache->setLogger(new StdClass);
}
+ public function testLoadPartialCascading()
+ {
+ $loader = new Mustache_Loader_ArrayLoader(array(
+ 'foo' => 'FOO',
+ ));
+
+ $mustache = new Mustache_Engine(array('loader' => $loader));
+
+ $tpl = $mustache->loadTemplate('foo');
+
+ $this->assertSame($tpl, $mustache->loadPartial('foo'));
+
+ $mustache->setPartials(array(
+ 'foo' => 'f00',
+ ));
+
+ // setting partials overrides the default template loading fallback.
+ $this->assertNotSame($tpl, $mustache->loadPartial('foo'));
+
+ // but it didn't overwrite the original template loader templates.
+ $this->assertSame($tpl, $mustache->loadTemplate('foo'));
+ }
+
public function testPartialLoadFailLogging()
{
$name = tempnam(sys_get_temp_dir(), 'mustache-test');
Please sign in to comment.
Something went wrong with that request. Please try again.