简体   繁体   English

下划线模板:将变量从父模板传递到子模板

[英]Underscore Templates: passing variables from a parent template to a child template

I have an underscore template that looks roughly like this: 我有一个下划线模板,大致如下所示:

<script type="text/template" id="list-template">
    <%= listHeaderTemplate(); %>
    stuff that
    displays a list
    <%= listFooterTemplate(); %>
</script>

<script type="text/template" id="list-footer-template">
    <%= foo %>
</script>

<script>
listTemplate = _.template($("#list-template").html());
listHeaderTemplate = _.template($("#list-header-template").html());
listFooterTemplate = _.template($("#list-footer-template").html());
</script>

What I want to do is to pass the full set of variables that are passed to listTemplate() into listFooterTemplate(). 我要做的是将传递给listTemplate()的完整变量集传递到listFooterTemplate()中。 For example, 例如,

listTemplate({foo: "foo"});

I can do this by modifying the calls to listHeaderTemplate() / listFooterTemplate() in list-template to package the locals into a dictionary (ie listFooterTemplate({foo: foo});) but that seems rather onerous, particularly when a large set of variables are in use, and it requires me to know inside of list-template which variables list-footer-template might use. 我可以通过在列表模板中修改对listHeaderTemplate()/ listFooterTemplate()的调用来将本地人打包到字典中(即listFooterTemplate({foo:foo});)来做到这一点,但这似乎相当麻烦,尤其是当设置了较大的集合时变量正在使用中,这需要我知道列表模板内部可能使用的变量list-footer-template。

When you compile an Underscore template, Underscore turns your template inside out and builds a function that looks, more or less, like this: 当您编译Underscore模板时,Underscore会将您的模板内翻,并构建一个看起来或多或少如下的函数:

function(obj) {
    // Some set up...
    with(obj || {}) {
        // The template as JavaScript...
    }
    return theTemplateText;
}

You can't depend on the parameter being called obj , that's bound to break soon or later. 您不能依赖于称为obj的参数,该参数肯定会迟早中断。 You should have safe access to the arguments object though. 但是,您应该可以安全地访问arguments对象。 Having arguments allows you to call other functions with exactly the same arguments as the current function without having to know what the arguments are, you just use apply . 具有arguments允许您使用与当前函数完全相同的参数来调用其他函数,而不必知道参数是什么,只需使用apply

If you have listHeaderTemplate and listFooterTemplate available to your template, you can simply say: 如果您的模板有可用的listHeaderTemplatelistFooterTemplate ,则可以简单地说:

<script type="text/template" id="list-template">
    <%= listHeaderTemplate.apply(this, arguments) %>
    stuff that
    displays a list
    <%= listFooterTemplate.apply(this, arguments) %>
</script>

An easy way is to pass those functions as arguments to your template: 一种简单的方法是将这些函数作为参数传递给模板:

var listTemplate       = _.template($("#list-template").html());
var listHeaderTemplate = _.template($("#list-header-template").html());
var listFooterTemplate = _.template($("#list-footer-template").html());

var html = listTemplate({
    foo: 11,
    listHeaderTemplate: listHeaderTemplate,
    listFooterTemplate: listFooterTemplate
});

Demo: http://jsfiddle.net/ambiguous/vwjm1kta/ 演示: http//jsfiddle.net/ambiguous/vwjm1kta/

Alternatively, one could pass the data object via JSON.stringify and a HTML data-* attribute. 或者,可以通过JSON.stringify和HTML data- *属性传递数据对象。

<script type="text/template" id="list-template">
<div id="my-list-template" data-us-tmpl-data="<%= JSON.stringify(data) %>">
</div>
</script>

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

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