2014-10-17 227 views
0

我有'postlist'模板上的名稱列表。這些是創建帖子的人的「用戶名」。我創建了一個href鏈接,以便當用戶點擊該名稱時,它們被路由/定向到一個新的模板'viewpost'並能夠查看完整的文章文檔。流星:模板內的渲染模板

但是,我希望將'viewpost'模板渲染到我已將{{> yield}}放入'postlist'模板中的位置。如何配置layourTemplate,因爲我已經有一個應用程序的另一部分工作。

當然,這個帖子文件顯然應該根據用戶點擊的名字來改變。

想象流星todos的例子。根據您點擊「加入」還是「登錄」,相關模板會被渲染。

所以我到目前爲止如下。

postlist.html(顯示發佈文檔中'全名'字段的列表)。

<template name="postlist"> 
<div class="container"> 
<div class="col-sm-3"> 
    {{#each post}} 
    <li><a href="{{pathFor 'viewpost'}}" >{{fullname}}</a></li> 
    {{/each}} 
</div> 
</div> 
{{> yield}} 
</template> 

router.js

Router.map(function(){ 
this.route('postlist',{ 
    path: '/postlist', 
    template: 'postlist', 
    waitOn: function(){ 
    return Meteor.subscribe('posts'); 
    }, 
    data: function(){ 
    return {post: Posts.find({})}; 
} 
}); 
this.route('viewpost',{ 
    path: '/viewpost/:_id', 
    template: 'viewpost', 
    waitOn: function(){ 
    return Meteor.subscribe('posts'); 
    }, 
    data: function() { return Posts.findOne(this.params._id); } 
}); 
}); 
+0

您可以創建'yield region ='post_body''並呈現到該區域。在這裏閱讀更多信息http://www.manuel-schoebel.com/blog/iron-router-tutorial – yoK0 2014-10-17 18:36:58

回答

1

由於似乎沒有太大的「viewpost」模板和「postlist」模板之間的內容重疊的,我認爲它實際上是更清潔只需使用{{> viewpost currentPost}}代替產量,並以編程方式設置currentPost。例如,在postlist模板,你可以有一個currentPost變量返回的數據當前職位,像這樣:

Template.postlist.helpers({ 
    currentPost: function() { 
     return Posts.findOne(Session.get('currentPost')); 
    } 
}); 

,當你點擊在postlist模板的鏈接剛剛設置的會議。

+1

'{{> Template.dynamic template ='viewpost'data = currentPost}}'是'{{ > viewpost currentPost}}'。帶有'Template.dynamic'的點有一個動態模板。 – 2014-10-17 19:25:13

+0

@ PeppeL-G你說得對,我正在考慮在我自己的應用程序中使用一個稍微不同的用例。編輯,謝謝! :) – mark 2014-10-17 19:29:35