2012-12-18 75 views
2

我正在使用Meteor,Handlebars和Backbone創建多頁應用程序。我有一個使用主幹建立的路由器,它設置了一個會話變量,currentPage。我如何根據currentPage的值呈現不同的模板?基於會話變量的渲染模板

有人告訴我,我可以創建一個模板幫助函數,可以做到這一點,但我不知道如何解決這個問題。

回答

1

如果當前頁是全球性的網頁存儲爲字符串,然後我希望這個工作:

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

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

的最好的選擇是用鐵路由器,但你不希望這裏是一個很好的模式更改模板:

//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函數有條件地更改模板。

+0

我在Meteor 0.9.4中試過這個,但它給出了錯誤: Tracker重新計算函數的異常:Error:No such template:mainTemplate –