【问题标题】:How does Partials / Template inheritance work in Mustache?部分/模板继承如何在 Mustache 中工作?
【发布时间】:2015-01-05 10:03:04
【问题描述】:

我不太了解 Mustache / Hogan.js 的部分/模板继承。正如herehere 所定义的那样,您似乎必须有两个不同的文件才能使其工作。我在我的(客户端)页面中使用它如下:

<template id="server-template"> {{#servers}} <b>some html and {{> some other which I dont understand how to use}}</b> {{/servers}} </template> <template id="room-template"> I want this in up there. (in the "partials" tag) </template>

感谢您的帮助。我用这个编译它们:

var source = $('#room-template').html(), compiled = Hogan.compile(source) $('#main').html(compiled.render(data))

这可能吗?

【问题讨论】:

    标签: javascript mustache hogan.js


    【解决方案1】:

    docs 声明您必须单独编译您的部分,然后将它们传递给 render 函数:

    在 mustache.js 中,partials 对象可以作为第三个对象传递 Mustache.render 的参数。对象应以 部分,它的值应该是部分文本。

    var roomTemplateSource = $('#room-template').html();
    var roomTemplate = Mustache.compile(roomTemplateSource);
    Mustache.render(template, view, {
      room: roomTemplate
    });
    
    <template id="server-template">
            {{#servers}}
            <b>some html and {{> room}}</b>
            {{/servers}}
    </template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 2011-11-14
      • 1970-01-01
      • 2020-04-18
      相关资源
      最近更新 更多