繁体   English   中英

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

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

Mustache模板引擎的PHP版本使您可以为模板和部件定义自定义加载程序。

我想使用它为我的模板创建名称空间,例如: {{>Post.article}}

使用自定义加载程序在PHP中实现起来很容易,但是当我使用JavaScript版本的Mustache时,似乎没有任何支持的加载程序。

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

目前,渲染模板时,我必须将所有部分的清单传递给Mustache。 这是一个问题,因为代码不知道模板所依赖的部分。

/**
 * 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);
};

显然已经支持此功能。 局部参数可以是一个函数,这将满足我的需求。

暂无
暂无

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

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