简体   繁体   English

如何使用类似PHP库的加载器加载Moustache模板/部分?

[英]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. Mustache模板引擎的PHP版本使您可以为模板和部件定义自定义加载程序。

I want to use this to create namespaces for my templates, example: {{>Post.article}} 我想使用它为我的模板创建名称空间,例如: {{>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. 使用自定义加载程序在PHP中实现起来很容易,但是当我使用JavaScript版本的Mustache时,似乎没有任何支持的加载程序。

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). 我需要以某种方式在Javascript中使用闭包回调函数来告诉Mustache在哪里可以找到部分(如果可能的话,可以找到模板,但是部分是当前的问题)。

Currently, I have to pass Mustache a list of all my partials when I render a template. 目前,渲染模板时,我必须将所有部分的清单传递给Mustache。 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. 局部参数可以是一个函数,这将满足我的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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