试图在splash.php中调用partials



尝试使用splash.php调用splash分部。我确信我搞砸了,因为文件似乎表明你可以做我想做的事。

    $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 }}');

我得到这个错误:

Fatal error: Uncaught exception 'Mustache_Exception_UnknownTemplateException' with message 'Unknown template: {{> 03-ups/00-two-up }}' in C:xampphtdocsgroganwordpresswp-contentthemesgrogan-themevendormustachemustachesrcMustacheLoaderFilesystemLoader.php:102
Stack trace: 
#0 C:xampphtdocsgroganwordpresswp-contentthemesgrogan-themevendormustachemustachesrcMustacheLoaderFilesystemLoader.php(82): Mustache_Loader_FilesystemLoader->loadFile('{{> 03-ups/00-t...') 
#1 C:xampphtdocsgroganwordpresswp-contentthemesgrogan-themevendormustachemustachesrcMustacheEngine.php(617): Mustache_Loader_FilesystemLoader->load('{{> 03-ups/00-t...') 
#2 C:xampphtdocsgroganwordpresswp-contentthemesgrogan-themevendormustachemustachesrcMustacheEngine.php(217): Mustache_Engine->loadTemplate('{{> 03-ups/00-t...') 
#3 C:xampphtdocsgroganwordpresswp-contentthemesgrogan-themepage-consignment.php(46): Mustache_Engine->render('{{> 03-ups/00-t...') 
#4 C:xampphtdocsgroganwordpresswp-includestempl in C:xampphtdocsgroganwordpresswp-contentthemesgrogan-themevendormustachemustachesrcMustacheLoaderFilesystemLoader.php on line 102

我正在使用patternlab来容纳我所有的部分,并将它们调用到wordpress模板中。不确定这是否重要。

tl;医生:你可能想用echo $m->render('03-ups/00-two-up')

Mustache使用"加载程序"来决定在您要求渲染哪个模板时进行渲染。具体来说,它使用了两个加载器:一个是用于所有render()调用的常规加载器,另一个是可选的部分加载器,用于渲染部分,正如您可能已经猜到的那样。如果您没有指定一个局部加载程序,它将返回到主加载程序。

默认情况下,Mustache使用字符串加载程序作为主加载程序。这就是为什么您可以开箱即用地调用$m->render('string with {{mustaches}}')。但是字符串加载程序并不适合多行模板,所以您通常需要指定一个文件系统加载程序。这需要一个基本目录,并根据名称从文件中加载模板。因此,如果调用$m->render('foo'),它将在文件系统加载程序的基本目录中查找一个名为foo.mustache的文件。

这就是您对它的配置,异常消息中有一个提示:它说Unknown template: {{> 03-ups/00-two-up }},意思是"我试图找到一个名为{{> 03-ups/00-two-up }}.mustache的文件,但没有":)

如果您将调用更改为实际的模板名称,它将起作用:

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

如果您真的想使用字符串加载程序作为主加载程序,但仍然指定一个部分文件系统加载程序,您可以显式添加它:

new Mustache_Engine([
  'partials_loader' => new Mustache_Loader_FilesystemLoader(...)
]);

最新更新