Javascript Meteor集合整体模板上下文
在meteor中,我可以设置各种模板帮助程序,如下所示:Javascript Meteor集合整体模板上下文,javascript,meteor,Javascript,Meteor,在meteor中,我可以设置各种模板帮助程序,如下所示: Template.story.title = function () { return "title"; }; <template name="story"> <h3>{{title}}</h3> <p>{{description}}</p> </template> Template.story.title=函数(){ 返回“标题”; }; {{titl
Template.story.title = function () {
return "title";
};
<template name="story">
<h3>{{title}}</h3>
<p>{{description}}</p>
</template>
Template.story.title=函数(){
返回“标题”;
};
{{title}}
{{description}}
这很好,但是,如果我有很多变量,我不想单独设置它们,我想将上下文传递给主模板
我该怎么做
Template.story.data = function () {
return {title:"title", description:"desc"};
};
<template name="story">
<h3>{{title}}</h3>
<p>{{description}}</p>
</template>
Template.story.data=函数(){
返回{title:“title”,description:“desc”};
};
{{title}}
{{description}}
那不行。谢谢您的方法绝对正确,但是没有按照您定义的方式使用模板变量。由于
Template.story.data
被定义为返回一个对象,您应该像使用对象一样使用它:
<template name="story">
<h3>{{data.title}}</h3>
<p>{{data.description}}</p>
</template>
{{data.title}
{{data.description}
瞧。当然,每个模板变量可以容纳的不仅仅是一个字符串。您可以在调用模板时设置其上下文:
{{> story data}}
Template.outerTemplate.data = function() {
return {title:"title", description:"desc"};
}
或者,您可以使用{{{#with}}
动态设置模板上下文:
{{#with data}}
{{title}}
{{/with}}
谢谢,这很有效,但是,难道没有一种方法可以设置一个主上下文吗?meteor docs说,
这是假定的上下文。应该有一种方法来设置此
而不是数据
。谢谢