Meteor 设置整体模板上下文

2023-12-28

在流星中我可以设置各种模板助手,如下所示:

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>

那是行不通的。谢谢


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

{{> story data}}

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

或者你可以只使用{{#with}}动态设置模板上下文:

{{#with data}}
  {{title}}
{{/with}}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Meteor 设置整体模板上下文 的相关文章

随机推荐