您可以通過添加新的partials loader實現輕鬆完成此操作。你可以做一個「別名加載器」,它存儲這些模板的引用:
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);
我在過去的幾天裏是AFK。感謝您的回答,這就是我正在尋找的! – feketegy 2013-02-20 13:38:54