在meteor中,我可以像这样设置各种模板助手:
Template.story.title = function () {
return "title";
};
<template name="story">
<h3>{{title}}</h3>
<p>{{description}}</p>
</template>
哪个好,但如果我有很多变量我不想单独设置它们,我想将上下文传递给主模板。
我该怎么做?
Template.story.data = function () {
return {title:"title", description:"desc"};
};
<template name="story">
<h3>{{title}}</h3>
<p>{{description}}</p>
</template>
这不起作用。感谢
答案 0 :(得分:12)
您可以在调用时设置模板的上下文:
{{> story data}}
Template.outerTemplate.data = function() {
return {title:"title", description:"desc"};
}
或者您可以使用{{#with}}
动态设置模板上下文:
{{#with data}}
{{title}}
{{/with}}
答案 1 :(得分:5)
您绝对是正确的方式,但却错过了按照您定义的方式使用模板变量。由于Template.story.data
被定义为返回一个对象,所以你应该像对象一样使用它:
<template name="story">
<h3>{{data.title}}</h3>
<p>{{data.description}}</p>
</template>
VOILÀ。当然,每个模板变量都可以包含多个字符串。