基于会话变量呈现模板

时间:2012-12-18 23:00:16

标签: backbone.js meteor handlebars.js

我使用Meteor,Handlebars和Backbone来创建多页面应用程序。我使用主干设置路由器,设置会话变量currentPage。如何根据currentPage的值

呈现不同的模板

有人告诉我,我可以创建一个模板辅助函数,但是我不确定如何处理这个问题。

3 个答案:

答案 0 :(得分:1)

如果currentPage是全局的并且页面存储为字符串,那么我希望这可以工作:

Handlebars.registerHelper('currentPageIs',function(page){
    return currentPage == page;
});

// and in the html:
{{#if currentPageIs '/posts'}}
    {{> posts}}
{{else}}
    {{> homepage}}
{{/if}}

答案 1 :(得分:0)

这里有一个非常简单的解决方案:https://gist.github.com/3221138

但我强烈建议安装meteor-router。这将要求您先安装meteorite

另外,要知道roadmap上有正式的路由解决方案。

答案 2 :(得分:0)

最好的选择是使用Iron-Router,但是你不希望这是一个很好的模式来改变模板:

//HTML
<body>
  {{>mainTemplate}}
</body>

//JS Client Initially
var current = Template.initialTemplate;
var currentDep = new Deps.Dependency;

Template.mainTemplate = function()
{
  currentDep.depend();
  return current;
};

function setTemplate( newTemplate )
{
    current = newTemplate;
    currentDep.changed();
};

//Later
setTemplate( Template.someOtherTemplate );

我不知道如何检查路线,但如果可以的话,您可以使用建议的setTemplate功能有条件地更改模板。