2012-11-28 36 views
3

我的流星應用程序有一個base.html文件的文件是這樣的:如何在流星中的幾個模板之間交替?

<head> 
    <title>MyApp</title> 
</head> 

<body> 
    <template name="bodyContent"> 
     {{subContent}} 
    </template> 
</body> 

接下來,我定義了三個不同的模板:

<template name="templateA">Some text</template> 
<template name="templateB">Bla bla bla</template> 
<template name="templateC">Your highscore is {{score}}</template> 

我怎樣才能讓{{子內容}}所以它能夠在運行時在這三個模板中的一個之間進行切換?

喜歡的東西:

currentTemplateUsed = 'templateB' 

Template.bodyContent.subContent = Template[currentTemplateUsed] 

但是,這並不工作。是否有其他方法來實現此功能?

回答

2

您應該使用meteor router,它能夠動態更改模板。可能的缺點是,它只能更改一次一個模板。

另外,還有對流星的車把上的位信息的: https://github.com/meteor/meteor/wiki/Handlebars

基本上,你必須使用模板作爲一個功能,並且爲了將數據傳遞給它的手把解析它,就像這樣:

var currentTemplateUsed = 'templateC'; 

var data = { 
    score : 12 
} 

Template.bodyContent.subContent = Template[currentTemplateUsed](data); 

返回Your highscore is 12

0

<body> ... </body>模板內似乎不工作,我不能內傳遞數據。我可能是錯的,但我必須把它放在外面並使用:

<body> 
    {{> body_content}} 
</body>