简体   繁体   English

在Mustache中,部分/模板继承如何工作?

[英]How does Partials / Template inheritance work in Mustache?

I don't really understand partials / template inheritance from Mustache / Hogan.js. 我不是很了解Mustache / Hogan.js的局部/模板继承。 As defined here and here you seem to must have two different files to make that work. 如此此处所定义,您似乎必须具有两个不同的文件才能使该工作正常进行。 I use it as follows in my (client side) page: 我在(客户端)页面中按如下方式使用它:

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

Thanks for helping. 感谢您的帮助。 I compile them with this: 我用这个编译它们:

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

Is that even possible? 那有可能吗?

The docs state that you must compile your partials separately, and then pass them to the render function: docs声明您必须分别编译部分,然后将它们传递给render函数:

In mustache.js an object of partials may be passed as the third argument to Mustache.render. 在mustache.js中,部分对象可以作为第三个参数传递给Mustache.render。 The object should be keyed by the name of the partial, and its value should be the partial text. 该对象应以部分名称作为关键字,其值应为部分文本。

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>

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

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