Mustache Templating:嵌套模板 [英] Mustache Templating: nested templates

查看:113
本文介绍了Mustache Templating:嵌套模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在胡子中使用嵌套模板?有没有办法做同样的事情?

How can I use a nested template within mustache? Is there a way to do the same?

var tmpl="{{#data}} 
{{values}}
Name: {{name}}
//{{another_templ({{name.value}})}}
{{/values}}
{{/data}}"

希望你们有问题。我没有为js有效性添加转义字符,因为代码被分成不同的行。

Hope you guys got the question. I have not added the escape character for js validity since code is split across different lines.

推荐答案

我已经做了一个嵌套的例子模板结束于 jsFiddle
详细信息如下:

I have made an example of nested templates over at jsFiddle. Here it is in detail:

首先,设置HTML

<div class="main"><!-- content here --></div>

<script type="text/html" id="tpl">
    {{#data}}
        {{#names}}
            Name: {{name}}
            {{#nested}}{{name}}{{/nested}}<br>
        {{/names}}
    {{/data}}
</script>

<script type="text/html" id="tpl-nested">
    &mdash; <b>{{name}}</b>
</script>​

然后是javascript(使用jQuery)

Then the javascript (using jQuery)

function renderNested(template_string, translate) {
    return function() {
        return function(text, render) {
            return Mustache.to_html(template_string, translate(render(text)));
        };
    };
}

var template = $("#tpl").html();

var nested_template = $("#tpl-nested").html();

var model = {
    data: {
        names: [
            { name: "Foo" },
            { name: "Bar" }
        ],
        nested: renderNested(nested_template, function(text) {
            return { name: text };
        })
    }
};

var result = Mustache.to_html(template, model);

$(".main").html( result );

这篇关于Mustache Templating:嵌套模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆