您可以通过添加新的局部加载器实现轻松地做到这一点。您可以制作一个“别名加载器”,用于存储这些模板引用:
class FilesystemAliasLoader extends Mustache_Loader_FilesystemLoader implements Mustache_Loader_MutableLoader
{
private $aliases = array();
public function __construct($baseDir, array $aliases = array())
{
parent::__construct($baseDir);
$this->setTemplates($aliases);
}
public function load($name)
{
if (!isset($this->aliases[$name])) {
throw new Mustache_Exception_UnknownTemplateException($name);
}
return parent::load($this->aliases[$name]);
}
public function setTemplates(array $templates)
{
$this->aliases = $templates;
}
public function setTemplate($name, $template)
{
$this->aliases[$name] = $template;
}
}
然后,您可以将其设置为部分加载程序:
$partials = array(
'sidebar' => 'folder1/somefile'
);
$mustache = new Mustache_Engine(array(
'loader' => new Mustache_Loader_FilesystemLoader('path/to/templates'),
'partials_loader' => new FilesystemAliasLoader('path/to/partials'),
'partials' => $partials,
));
...或者您可以将别名传递给加载程序构造函数,或者您甚至可以稍后在加载程序或引擎实例上设置它们:
$loader = new FilesystemAliasLoader('path/to/partials', $partials);
$loader->setTemplates($partials);
$mustache->setPartials($partials);