简体   繁体   English

流星设置整体模板上下文

[英]Meteor set overall template context

In meteor I can set various template helpers like this: 在meteor中我可以像这样设置各种模板助手:

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. 这很棒但是,如果我有很多变量我不想单独设置它们,我想将上下文传递给主模板。

How do I do that? 我怎么做?

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

That doesn't work. 这不起作用。 THanks 谢谢

You can set the context of the template when you call it: 您可以在调用时设置模板的上下文:

{{> story data}}

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

Or you can just use {{#with}} to set the the template context on the fly: 或者您可以使用{{#with}}设置模板上下文:

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

You are absolutely on the right way but missed to use your template variable the way you defined it. 你绝对是正确的方式,但错过了你定义它的方式使用你的模板变量。 As Template.story.data is defined to return an object, you should use it like an object: 由于Template.story.data被定义为返回一个对象,所以你应该像对象一样使用它:

<template name="story">
  <h3>{{data.title}}</h3>
  <p>{{data.description}}</p>
</template>

Voilá. 瞧。 Of course every template variable can hold more than just a string. 当然,每个模板变量都可以包含多个字符串。

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

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