簡體   English   中英

預編譯模板中的Hogan.js局部

[英]Hogan.js partials in pre-compiled template

是否可以在已編譯的模板中包含局部變量? 由於在編譯對象中引用了部分名稱,因此看起來似乎有可能,但是我不知道如何使它起作用。

我正在服務器端(node.js)上預編譯Hogan.js模板,並使其在客戶端上可用。 這是模板的片段:

<ul class="log-{{id}}">
  {{#entries}}
    {{> entry}}
  {{/entries}}
</ul>

編譯完模板后,我會在對象中看到一個帶有<entry0鍵的partials屬性。

我可以使用以下方法在客戶端上渲染模板:

var data = {id: 11, entries: [{ id: 1, name: 'Entry 1'}, {id: 2, name: 'Entry 2'}]};

template = new Hogan.Template(compiledTemplate);
template.render(data);

該模板可以很好地呈現,但是在{{#entries}} {{/entries}}塊中什么也沒有。 部分本身也已預編譯,可在客戶端使用。 我試圖通過幾種不同的方式來傳遞它,包括:

 template.render(data, {partials: { entry: compiledEntryTemplate }});

所有跡象似乎都表明這應該是可能的,但我只是無法弄清楚,也找不到任何指出如何操作的文檔。 我正在使用Hogan.js 3.0.1

設法弄清楚了。 以下內容將使已編譯的部分文件可以在已編譯的模板中訪問。

 template.render(data, { entry: new Hogan.Template(compiledEntryTemplate) });

@TJ,您可以看看hogan.js測試套件( https://github.com/twitter/hogan.js/blob/master/test/index.js )。 在那里,您將找到一個名為“ Partial Basic”的測試用例,其內容為

var partialText = "this is text from the partial--the magic number {{foo}} is from a variable";
  var p = Hogan.compile(partialText);

  var text = "This template contains a partial ({{>testPartial}})."
  var t = Hogan.compile(text);

  var s = t.render({foo: 42}, {testPartial: p});
  is(s, "This template contains a partial (this is text from the partial--the magic number 42 is from a variable).", "partials work");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM