2012-11-19 27 views
1

我在設置不同網頁的簡單網站時遇到問題並且保持乾燥。DRY用於顯示不同網址的流星模板

我已經設置了一切,所以我的網址的最後一個片段是需要加載到網頁內容部分的模板的名稱。我現在要做的就是根據url在特定位置加載該模板。

在任何的例子,他們這樣做:

{{#if showCreateDialog}} 
    {{> createDialog}} 
    {{/if}} 

    {{#if showInviteDialog}} 
    {{> inviteDialog}} 
    {{/if}} 

我想一起做的

{{> {{template_name}} }} 

可悲的是,這並不工作線的東西。無論是

{{{content}}} 

Template.content.content = function() { 
var url_frag = Session.get("url_frag"); 
return Template[url_frag](); 
} 

這沒有工作:我想這一點。請幫忙!

編輯:

哼。也許,我的錯誤是在不加載模板,但在拍攝的網址:

var TodosRouter = Backbone.Router.extend({ 
    routes: { 
     "*url": "main" 
    }, 
    main: function (url) { 
     Session.set("url", url.split('/')) 
    } 
}); 

時url_frag是不確定的,我得到的錯誤出現...

var url_frag = Session.get("url_frag"); 

最初,這個工作,但在改變網頁,它失敗...

+0

我覺得你的模板使用[名](),結合{{{}}}應該工作。 下面是一個使用示例:https://github.com/meteor/meteor/blob/master/packages/accounts-ui-unstyled/login_buttons_dialogs.js#L223和https://github.com/meteor/ meteor/blob/master/packages/accounts-ui-unstyled/login_buttons_dialogs.html#L71 – avital

+0

感謝您的幫助。檢查我的編輯。我可能已經指出了錯誤在別處。 – chet

回答

3

解決。我只是在HTML左主幹出來的

Template.content.content = function() { 
var url = window.location.pathname.split('/'); 
var url_frag = url.pop() 
return Template[url_frag](); 

然後:

<template name="content"> 
    {{{content}}} 
</template> 
+0

你可以接受你自己的答案。 – Rahul

+0

但是這會導致整頁重新加載,而不是? – user1680104