流星设置整体模板上下文

问题描述:

在meteor中,我可以像这样设置各种模板助手:

In meteor I can set various template helpers like this:

Template.story.title = function () {
  return "title";
};
<template name="story">
  <h3>{{title}}</h3>
  <p>{{description}}</p>
</template>

哪个好,但是,如果我有很多变量,我不想单独设置它们,我想将上下文传递给主模板。

Which is great but, if I have a lot of variables I wouldn't want to set them individually, I want to pass the context to the main template.

我该怎么做?

Template.story.data = function () {
  return {title:"title", description:"desc"};
};
<template name="story">
  <h3>{{title}}</h3>
  <p>{{description}}</p>
</template>

这不起作用。谢谢

您可以在调用时设置模板的上下文:

You can set the context of the template when you call it:

{{> story data}}

Template.outerTemplate.data = function() { 
  return {title:"title", description:"desc"};
}

或者你可以使用 {{#with} } 即时设置模板上下文:

Or you can just use {{#with}} to set the the template context on the fly:

{{#with data}}
  {{title}}
{{/with}}