簡體   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