Reputation: 2329
I building my next portfolio site. How I can place handlebars expressions inside an #each
loop? This is an express application generated by express-generator. I'm using the express-handlebars
NPM package:
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;
What should return:
<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>
But what it's returning:
<li><img src="image-1.jpg" alt=""></li>
<li><img src="image-2.jpg" alt=""></li>
<li><img src="image-3.jpg" alt=""></li>
Worked fine until I moved the /portfolio/weddings/
folder structure from the .hbs file into the router.
Repo: https://github.com/DJviolin/horgalleryNode
Upvotes: 1
Views: 1355
Reputation: 29172
You need access to parent's property imagesFolder
:
{{#each images}}
<li><img src="{{../imagesFolder}}{{this}}.jpg" alt=""></li>
{{/each}}
Upvotes: 4