简体   繁体   中英

How to load Mustache templates/partials using a loader like in PHP library?

The PHP version of the Mustache template engine allows you to define custom loaders for templates and partials.

I want to use this to create namespaces for my templates, example: {{>Post.article}}

That's easy to implement in PHP using a custom loader, but when I use the Javascript version of Mustache there doesn't appear to be any kind of loader supported.

What I need is someway of using a closure callback in Javascript to tell Mustache where it can find partials (and templates if possible, but partials are the current issue).

Currently, I have to pass Mustache a list of all my partials when I render a template. That's a problem because the code doesn't know what partials a template depends on.

/**
 * High-level method that is used to render the given `template` with
 * the given `view`.
 *
 * The optional `partials` argument may be an object that contains the
 * names and templates of partials that are used in the template. It may
 * also be a function that is used to load partial templates on the fly
 * that takes a single argument: the name of the partial.
 */
Writer.prototype.render = function (template, view, partials) {
  var tokens = this.parse(template);
  var context = (view instanceof Context) ? view : new Context(view);
  return this.renderTokens(tokens, context, partials, template);
};

Apparently this feature is already supported. The partials parameter can be a function and this will do what I need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM