如何在流星0.8版本中加载动态模板



我想动态加载模板,而不使用路由器包,而是使用会话。功能是,我有两个模板"注册"one_answers"登录"。当我点击登录时,它必须打开登录页面模板,与注册相同。我通过这个代码来解决问题,但没有成功。

<body>
    {{> content}}
</body>
<template name="content">
    {{renderTemplate}}
</template>

内容助手

Template.content.helpers({
    'renderTemplate': function(){
        return new Handlebars.SafeString(
            Template[Session.get('currentTemplate')]({dataKey: 'someValue'})
    }
})

请帮助解决方案。提前感谢

当您升级到0.8时,您应该已经收到一个必读页面的URL。这个页面告诉你模板不再是函数,不会返回字符串:

https://github.com/meteor/meteor/wiki/Using-Blaze#templatefoo-is-not-a-function and does-not-return-a-string

这就是为什么这不起作用:

Template[Session.get('currentTemplate')]({dataKey: 'someValue'})

您只需返回模板对象即可。,但是在调用renderTemplate之前,您需要以某种方式更改数据上下文。我不能告诉你怎么做,因为我不知道所有的模板都需要什么数据。

Template.content.helpers({
    'renderTemplate': function(){
        return Template[Session.get('currentTemplate')];
   }
});

更新:请确保使用链接页面中所述的{{> renderTemplate}},而不是{{renderTemplate}}

最新更新