将数据传递到动态模板

2024-01-07

随着meteor更新到0.8,我的旧代码停止工作。

Handlebars.registerHelper('getTemplate', function(id, context) {
    return Template[id](context);
}); 

<template name="main">
    ....
    {{{getTemplate templateName context}}}
    ....
</template>

//somewhere in other template 
Template.main.context = {name:value};

这样我就能够使用自定义数据渲染自定义模板。现在我找不到路过去context到动态模板。与火焰两者templateName and context未定义。有什么建议吗?


流星 >= 0.8.2

您可以使用UI.dynamic帮助器使用动态指定的上下文渲染模板。欲了解更多详情,请查看这个问题 https://github.com/meteor/meteor/issues/2123.

流星

这两个问题均在这一页 https://github.com/meteor/meteor/wiki/Using-Blaze在流星维基中。

  1. Handlebars.registerHelper is now UI.registerHelper正如所见here https://github.com/meteor/meteor/wiki/Using-Blaze#handlebars-namespace-deprecated.

  2. 显示了如何动态渲染模板的示例here https://github.com/meteor/meteor/wiki/Using-Blaze#templatefoo-is-not-a-function-and-does-not-return-a-string.


update

实际上,考虑到需求,解决方案对我来说似乎并不是很明显。如果您愿意使用会话变量来设置模板名称和上下文,并且主模板中只有一个动态模板。你可以这样做:

<body>
  {{> main}}
</body>

<template name="main">
  {{> getTemplate context}}
</template>

<template name="dogs">
  <p>There are {{animals}} dogs!</p>
</template>

<template name="cats">
  <p>There are {{animals}} cats!</p>
</template>
Session.setDefault('templateName', 'dogs');
Session.setDefault('templateContext', {animals: 10});

Template.main.getTemplate = function() {
  return Template[Session.get('templateName')];
};

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

将数据传递到动态模板 的相关文章

随机推荐