I want to pass an argument to a template I'm invoking in a loop:
我想将一个参数传递给我在循环中调用的模板:
<template name="show_people">
<div class="panel-body">
{{#each people}}
<div>
{{>person }}
{{>person doing="running up the hill"}}
</div>
{{/each}}
</div>
</template>
<template name="person">
<h3>{{name}} is {{doing}}</h3>
</template>
The helper javascript:
助手javascript:
Template.show_people.helpers({
people: function() { return [{ name: 'Jack' },{ name: 'Jill' }]; }
});
Adding the 'doing' argument to the template seems to clobber the context of the loop item. Here's what I'm getting back:
将“执行”参数添加到模板似乎破坏了循环项的上下文。这是我要回来的:
Jack is
is running up the hill
Jill is
is running up the hill
I would like the person template to be able to access both the argument and the context. How can this be accomplished?
我希望person模板能够访问参数和上下文。如何实现这一目标?
2 个解决方案
#1
0
Quick hack : pass a name argument to your template that is copied from the parent context.
快速入侵:将名称参数传递给从父上下文复制的模板。
{{> person name=name doing="running up the hill"}}
#2
0
The accepted answer solves your problem but does not give an explanation as to why you are experiencing the issue that you are. If you take a look at this article under the Template Includes section header, you will find the reason why.
接受的答案可以解决您的问题,但不会解释您遇到问题的原因。如果您在“模板包含”部分标题下查看本文,您将找到原因。
Basically, just inside of your {{#each people}}{{/each}}
code block, the data context of any contained templates will be the list item within the people
collection. In the case of your first two code snippets, the two data contexts for the two instances of people
would be {name: "Jack"}
and {name: "Jill"}
, which is the reason why you are seeing Jack is
and Jill is
being printed out for these two template instances. The data context does not contain a doing
parameter.
基本上,就在您的{{#each people}} {{/ each}}代码块内部,任何包含的模板的数据上下文都将是人员集合中的列表项。对于前两个代码片段,两个人的实例的两个数据上下文将是{name:“Jack”}和{name:“Jill”},这就是为什么你看到Jack和Jill的原因正在为这两个模板实例打印出来。数据上下文不包含执行参数。
When you reference your person
template in the second way ({{> person doing='running up the hill'
), the data context for that template instance is reset and a whole new data context only with the specified parameters is created. For both instances of your person
template, the data context would be {doing: "running up the hill"}
, which is why you are seeing is running up the hill
being printed out twice.
当您以第二种方式引用人员模板时({{> person doing ='跑上山'),将重置该模板实例的数据上下文,并创建仅具有指定参数的全新数据上下文。对于你的人员模板的两个实例,数据上下文将是{做:“跑上山”},这就是为什么你看到正在运行两次打印的山。
So as you can see, the data context setting is not additive, but rather, it is exclusive. Either the data context is the parent data context for the block of code where a given template reference appears, or it is an overridden data context consisting of all of the parameters defined in the template reference. The reason why the accepted answer works is because you are combining both data contexts into a single overridden data context to be used in the person
template.
如您所见,数据上下文设置不是附加的,而是独占的。数据上下文是出现给定模板引用的代码块的父数据上下文,或者是由模板引用中定义的所有参数组成的重写数据上下文。接受的答案之所以有效,是因为您将两个数据上下文组合成一个要在人员模板中使用的重写数据上下文。
#1
0
Quick hack : pass a name argument to your template that is copied from the parent context.
快速入侵:将名称参数传递给从父上下文复制的模板。
{{> person name=name doing="running up the hill"}}
#2
0
The accepted answer solves your problem but does not give an explanation as to why you are experiencing the issue that you are. If you take a look at this article under the Template Includes section header, you will find the reason why.
接受的答案可以解决您的问题,但不会解释您遇到问题的原因。如果您在“模板包含”部分标题下查看本文,您将找到原因。
Basically, just inside of your {{#each people}}{{/each}}
code block, the data context of any contained templates will be the list item within the people
collection. In the case of your first two code snippets, the two data contexts for the two instances of people
would be {name: "Jack"}
and {name: "Jill"}
, which is the reason why you are seeing Jack is
and Jill is
being printed out for these two template instances. The data context does not contain a doing
parameter.
基本上,就在您的{{#each people}} {{/ each}}代码块内部,任何包含的模板的数据上下文都将是人员集合中的列表项。对于前两个代码片段,两个人的实例的两个数据上下文将是{name:“Jack”}和{name:“Jill”},这就是为什么你看到Jack和Jill的原因正在为这两个模板实例打印出来。数据上下文不包含执行参数。
When you reference your person
template in the second way ({{> person doing='running up the hill'
), the data context for that template instance is reset and a whole new data context only with the specified parameters is created. For both instances of your person
template, the data context would be {doing: "running up the hill"}
, which is why you are seeing is running up the hill
being printed out twice.
当您以第二种方式引用人员模板时({{> person doing ='跑上山'),将重置该模板实例的数据上下文,并创建仅具有指定参数的全新数据上下文。对于你的人员模板的两个实例,数据上下文将是{做:“跑上山”},这就是为什么你看到正在运行两次打印的山。
So as you can see, the data context setting is not additive, but rather, it is exclusive. Either the data context is the parent data context for the block of code where a given template reference appears, or it is an overridden data context consisting of all of the parameters defined in the template reference. The reason why the accepted answer works is because you are combining both data contexts into a single overridden data context to be used in the person
template.
如您所见,数据上下文设置不是附加的,而是独占的。数据上下文是出现给定模板引用的代码块的父数据上下文,或者是由模板引用中定义的所有参数组成的重写数据上下文。接受的答案之所以有效,是因为您将两个数据上下文组合成一个要在人员模板中使用的重写数据上下文。