将数据传递到动态模板 [英] Pass data to a dynamic template

查看:94
本文介绍了将数据传递到动态模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着流星更新达到0.8,我的旧代码停止工作.

With meteor updates up to 0.8 my old code stopped working.

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传递给动态模板的方法.出现火焰时,templateNamecontext均未定义.有什么建议吗?

This way I was able to render a custom template with custom data. Now I can't find the way to pass context to the dynamic template. With blaze both templateName and context is undefined. Any advice?

推荐答案

流星> = 0.8.2

您可以使用UI.dynamic帮助器来渲染具有动态指定的上下文的模板.有关更多详细信息,请查看此问题.

Meteor >= 0.8.2

You can use the UI.dynamic helper render a template with a context which are both specified dynamically. For more details, check out this issue.

这两个问题都在流星Wiki中的此页面中得到解决.

Both of these issues are addressed on this page in the meteor wiki.

  1. Handlebars.registerHelper现在是UI.registerHelper,如这里.


更新

实际上,给定要求,对我来说解决方案似乎并不十分明显.如果您愿意使用会话变量来设置模板名称和上下文,并且在主模板中仅包含一个动态模板.您可以执行以下操作:


update

Actually, given the requirements, a solution doesn't seem very obvious to me. If you are willing to use session variables to set the template name and context, AND only have one dynamic template in your main template. You could do something like this:

<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');
};

这篇关于将数据传递到动态模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆