0

Trying to call a mustache partial using mustache.php. I'm certain I'm messing something up, because the documentation seems to express that you can do what I'm trying to do.

    $m = new Mustache_Engine(array(
    'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/patternlab-php-master/source/_patterns/02-organisms/'),
));

echo $m->render('{{> 03-ups/00-two-up }}');

I get this error:

Fatal error: Uncaught exception 'Mustache_Exception_UnknownTemplateException' with message 'Unknown template: {{> 03-ups/00-two-up }}' in C:\xampp\htdocs\grogan\wordpress\wp-content\themes\grogan-theme\vendor\mustache\mustache\src\Mustache\Loader\FilesystemLoader.php:102
Stack trace: 
#0 C:\xampp\htdocs\grogan\wordpress\wp-content\themes\grogan-theme\vendor\mustache\mustache\src\Mustache\Loader\FilesystemLoader.php(82): Mustache_Loader_FilesystemLoader->loadFile('{{> 03-ups/00-t...') 
#1 C:\xampp\htdocs\grogan\wordpress\wp-content\themes\grogan-theme\vendor\mustache\mustache\src\Mustache\Engine.php(617): Mustache_Loader_FilesystemLoader->load('{{> 03-ups/00-t...') 
#2 C:\xampp\htdocs\grogan\wordpress\wp-content\themes\grogan-theme\vendor\mustache\mustache\src\Mustache\Engine.php(217): Mustache_Engine->loadTemplate('{{> 03-ups/00-t...') 
#3 C:\xampp\htdocs\grogan\wordpress\wp-content\themes\grogan-theme\page-consignment.php(46): Mustache_Engine->render('{{> 03-ups/00-t...') 
#4 C:\xampp\htdocs\grogan\wordpress\wp-includes\templ in C:\xampp\htdocs\grogan\wordpress\wp-content\themes\grogan-theme\vendor\mustache\mustache\src\Mustache\Loader\FilesystemLoader.php on line 102

I'm using patternlab to house all my partials and calling them into wordpress templates. Not sure if that matters.

Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
Ashleigh
  • 35
  • 7

1 Answers1

0

tl;dr: You probably want to use echo $m->render('03-ups/00-two-up').

Mustache uses "loaders" to decide what template to render when you ask for one. Specifically, it uses two loaders: a regular one, used for all render() calls, and an optional partials loader, which is used to render partials, as you might have guessed. If you don't specify a partials loader, it falls back to the main one.

By default, Mustache uses a string loader for the main loader. This is why you can call $m->render('string with {{mustaches}}') out of the box. But a string loader isn't ideal for much more than one-line templates, so you usually want to specify a filesystem loader. This takes a base directory, and loads templates from files based on the name. So if you call $m->render('foo') it'll look for a file called foo.mustache in the filesystem loader's base directory.

This is what you configured it to do, and there's a hint in the exception message: it says Unknown template: {{> 03-ups/00-two-up }}, meaning "I tried to find a file called {{> 03-ups/00-two-up }}.mustache but there wasn't one" :)

If you change your call to the actual template name, it'll work:

echo $m->render('03-ups/00-two-up');

If you really want to use a string loader for your primary loader, but still specify a partials filesystem loader, you can explicitly add it:

new Mustache_Engine([
  'partials_loader' => new Mustache_Loader_FilesystemLoader(...)
]);
bobthecow
  • 5,047
  • 25
  • 27
  • I'm going to have to try that out... I think I recall removing the ">" from the render argument and it still didn't render the template. I will get back to you and let you know if that works. My situation here is kinda sticky as I have MULTIPLE partial folders, due to using patternlab, it breaks the partials down into 3 or so different folders. I didn't see anything in the documentation for multiple partial loaders, but maybe I didn't see it? I appreciate the creator helping me out though, this is cool =) – Ashleigh Jul 07 '15 at 13:58