Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 将数据传递到动态模板_Meteor_Meteor Blaze - Fatal编程技术网

Meteor 将数据传递到动态模板

Meteor 将数据传递到动态模板,meteor,meteor-blaze,Meteor,Meteor Blaze,随着meteor更新到0.8,我的旧代码停止工作 Handlebars.registerHelper('getTemplate', function(id, context) { return Template[id](context); }); <template name="main"> .... {{{getTemplate templateName context}}} .... </template> //somewhere

随着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};
handlebar.registerHelper('getTemplate',函数(id,上下文){
返回模板[id](上下文);
}); 
....
{{{getTemplateTemplateName上下文}}}
....
//在其他模板中的某处
Template.main.context={name:value};
通过这种方式,我能够使用自定义数据呈现自定义模板。现在我找不到将
上下文
传递给动态模板的方法。使用blaze时,
templateName
context
都未定义。有什么建议吗

流星>=0.8.2 您可以使用
UI.dynamic
helper以动态指定的上下文呈现模板。有关更多详细信息,请查看

流星<0.8.2 这两个问题都在meteor wiki上得到了解决

  • handlebar.registerHelper
    现在是
    UI.registerHelper
    ,如图所示

  • 显示了如何动态渲染模板的示例


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

    
    {{>main}
    {{>getTemplateContext}
    有{{动物}狗

    有{{动物}猫

    Session.setDefault('templateName','dogs'); setDefault('templateContext',{animals:10}); Template.main.getTemplate=函数(){ 返回模板[Session.get('templateName'); }; Template.main.context=函数(){ return Session.get('templateContext'); };
    这是meteor core列表中提到的,MDG core dev正在Blaze上工作,因此他们肯定知道这一点,我希望在0.8.0之后很快会有一个补丁

    他还包括以下解决方法:

    var toHTMLWithData = function (kind, data) {
      return UI.toHTML(kind.extend({data: function () { return data; }}));
    };
    

    github票证有进一步的讨论和您可能会发现有用的替代代码片段。

    可能相关:当然,我在提问之前已经读过了。没用。是的,这比我想象的要难。我用一个使用会话变量的示例更新了答案。这可能不是您想要的,因为它不是很可重用的。@Serge在meteor 0.8.2中有一个名为
    UI.dynamic
    的新助手,可以帮助您解决问题。我用一个链接更新了答案的细节。