我正在使用Meteor,Handlebars和Backbone創建多頁應用程序。我有一個使用主幹建立的路由器,它設置了一個會話變量,currentPage
。我如何根據currentPage
的值呈現不同的模板?基於會話變量的渲染模板
有人告訴我,我可以創建一個模板幫助函數,可以做到這一點,但我不知道如何解決這個問題。
我正在使用Meteor,Handlebars和Backbone創建多頁應用程序。我有一個使用主幹建立的路由器,它設置了一個會話變量,currentPage
。我如何根據currentPage
的值呈現不同的模板?基於會話變量的渲染模板
有人告訴我,我可以創建一個模板幫助函數,可以做到這一點,但我不知道如何解決這個問題。
如果當前頁是全球性的網頁存儲爲字符串,然後我希望這個工作:
Handlebars.registerHelper('currentPageIs',function(page){
return currentPage == page;
});
// and in the html:
{{#if currentPageIs '/posts'}}
{{> posts}}
{{else}}
{{> homepage}}
{{/if}}
一個非常簡單的解決辦法是在這裏:https://gist.github.com/3221138
但我強烈建議安裝meteor-router。這將要求您首先安裝meteorite。
另外,知道官方路由解決方案是在roadmap。
的最好的選擇是用鐵路由器,但你不希望這裏是一個很好的模式更改模板:
//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
函數有條件地更改模板。
我在Meteor 0.9.4中試過這個,但它給出了錯誤: Tracker重新計算函數的異常:Error:No such template:mainTemplate –