PHP Mustache 2.1 partial download is NOT based on file name

Is there a way to load partial data based on an array of file name values?

Currently, if I write this one {{> sidebar}}, he will search views/sidebar.mustache. (based on the template loader class, where I can specify where to look for templates)

Ideally, I want it to {{> sidebar}}be a variable name, not a file name.

What I want to achieve is to look for a partial part of the sidebar that is not based on the file name if I go to the bootloader:

$partials = array(
    'sidebar' => 'folder1/somefile'
);

which translates to: views/folder1/somefile.mustache.

+5
source share
1 answer

, partials. " ", :

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:

$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);
+7

All Articles