Dar*_*ung 4 node.js express handlebars.js
今天早上,我想我最后会把一个快速的Express应用程序放在一起看看Handlebars.到目前为止我很享受(我觉得我更喜欢Handlebars和Jade),但我有一个问题.我正在玩的应用程序是(又一个)使用Mongo的基本CMS.在/ posts/index页面上,我正在检查并显示帖子:
{{#if posts}}
{{#each posts}}
<h4>{{title}}
<p>{{content}}</p>
{{/each}}
{{else}}
<p>There are no posts.</p>
{{/if}}
Run Code Online (Sandbox Code Playgroud)
这很简单并按预期工作,但我当时想知道在每个帖子的底部添加类似编辑按钮的内容.我想基于用户来调整这种可见性.如果登录(我正在使用Passport的本地策略),我可以通过以下方式轻松查看用户的详细信息:
{{#if user}}
{{user.name}}
{{/if}}
Run Code Online (Sandbox Code Playgroud)
但问题是,我似乎无法得到那个,如果用户检查工作中的每一个上面.在Handlebars中这可能吗?在Jade过去,我刚刚使用了类似的东西:
if(posts.length)
each post in posts
#{post.title}
if(user)
...
Run Code Online (Sandbox Code Playgroud)
为清楚起见,帖子页面的获取路径是:
router.get('/', function(req, res) {
Post.find({}, function(err, posts) {
res.render('posts/index', {
title: 'Posts',
user: req.user,
posts: posts,
});
});
});
Run Code Online (Sandbox Code Playgroud)
如上所述,我对Handlebars很新,所以我希望这是非常明显的事情.在此先感谢,我希望你们都有一个伟大的NYE!
Mat*_*ain 13
each一个if,each...else工作.user内部each,您实际上将尝试访问posts.user.您可以使用访问父上下文../.例如
{{#each posts}}
<h4>{{title}}
<p>{{content}}</p>
{{#if ../user}}
{{../user.name}}
{{/if}}
{{else}}
<p>There are no posts.</p>
{{/each}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4404 次 |
| 最近记录: |