流星:在模板内部渲染模板 [英] Meteor: Render template inside a template

查看:65
本文介绍了流星:在模板内部渲染模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在帖子列表"模板上有一个姓名列表.这些是创建帖子的人的用户名".我创建了一个href链接,以便当用户单击名称时,他们将被路由/定向到新模板"viewpost",并能够查看完整的帖子文档.

I have a list of names on 'postlist' template. These are 'usernames' of the person who created the post. I have created a href link so that when a user clicks on the name, they are routed/directed to a new template 'viewpost' and able to view the full post document.

但是,我希望渲染"viewpost"模板,而不是将"{{> yield}}"放在"postlist"模板中.如何配置layourTemplate,因为我已经为应用程序的另一部分工作了.

But, I would like the 'viewpost' template to get rendered where I have put {{> yield}} inside the 'postlist' template instead. How do I configure the layourTemplate because I already have one working for another part of the app.

当然,此后文档显然应该根据用户单击的名称进行更改.

Of course, this post document obviously should change according to which name the user clicks.

以流星待办事项为例.根据您单击加入"还是登录",将渲染相关模板.

Think Meteor todos example. Depending on whether you click 'join' or 'signin', the relevant templates gets rendered.

到目前为止,我所拥有的如下.

So what I have is so far is as follows.

postlist.html(显示邮政文档中全名"字段的列表).

postlist.html (showing list of the 'fullname' field in post documents).

<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.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); }
});
});

推荐答案

由于'viewpost'模板和'postlist'模板之间似乎没有太多内容重叠,因此我认为它实际上会更干净只需使用{{> viewpost currentPost}}代替yield,并以编程方式设置currentPost.例如,在帖子列表模板中,您可以具有currentPost变量,该变量返回当前帖子的数据,如下所示:

Since there doesn't seem to be much of a content overlap between the 'viewpost' template and the 'postlist' template, I think it will actually be cleaner to just use {{> viewpost currentPost}} in place of the yield, and set the currentPost programmatically. For example, in the postlist template, you can have a currentPost variable that returns the data for the current post, like so:

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

,只需单击发布列表模板中的链接即可设置会话.

and just set the Session when you click on the link in the postlist template.

这篇关于流星:在模板内部渲染模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆