最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor set overall template context - Stack Overflow

programmeradmin3浏览0评论

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.

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

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.

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

Share Improve this question asked Oct 22, 2012 at 6:17 HarryHarry 55k76 gold badges185 silver badges270 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

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 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 name="story">
  <h3>{{data.title}}</h3>
  <p>{{data.description}}</p>
</template>

Voilá. Of course every template variable can hold more than just a string.

发布评论

评论列表(0)

  1. 暂无评论