{{#each}}内的句柄表达式

时间:2016-02-08 16:32:30

标签: javascript node.js express handlebars.js

我正在构建我的下一个投资组合网站。如何将把手表达式放在#each循环中?这是由express-generator生成的快速应用程序。我正在使用express-handlebars NPM包:

views/home.hbs

{{#each images}}
  <li><img src="{{imagesFolder}}{{this}}.jpg" alt=""></li>
{{/each}}

routes/home.js

'use strict';

var express = require('express'),
    router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('home', {
    layout: 'main',
    centering: true,
    titleShown: false,
    title: 'Hi!',
    description: 'Home page',
    keywords: 'wedding,photography,film',
    bodyClass: 'horizontal',
    imagesFolder: '\/portfolio\/weddings\/',
    images: [
      'image-1',
      'image-2',
      'image-3'
    ]
  });
});

module.exports = router;

应该返回什么:

<li><img src="/portfolio/weddings/image-1.jpg" alt=""></li>
<li><img src="/portfolio/weddings/image-2.jpg" alt=""></li>
<li><img src="/portfolio/weddings/image-3.jpg" alt=""></li>

但是它又回来了:

<li><img src="image-1.jpg" alt=""></li>
<li><img src="image-2.jpg" alt=""></li>
<li><img src="image-3.jpg" alt=""></li>

工作正常,直到我将/portfolio/weddings/文件夹结构从.hbs文件移动到路由器中。

回购:https://github.com/DJviolin/horgalleryNode

1 个答案:

答案 0 :(得分:4)

您需要访问父级的imagesFolder

{{#each images}}
  <li><img src="{{../imagesFolder}}{{this}}.jpg" alt=""></li>
{{/each}}