【问题标题】:How to load Mustache templates/partials using a loader like in PHP library?如何使用 PHP 库中的加载器加载 Mustache 模板/部分?
【发布时间】:2014-01-01 16:50:53
【问题描述】:

Mustache 模板引擎的 PHP 版本允许您为模板和部分定义自定义加载器。

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

使用自定义加载器在 PHP 中很容易实现,但是当我使用 Javascript 版本的 Mustache 时,似乎不支持任何类型的加载器。

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

目前,当我渲染模板时,我必须向 Mustache 传递我所有部分的列表。这是一个问题,因为代码不知道模板依赖于哪些部分。

【问题讨论】:

    标签: javascript php mustache


    【解决方案1】:
    /**
     * 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);
    };
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多