Home Ask Login Register

Developers Planet

Your answer is one click away!

Lanti February 2016

Handlebars expression inside {{#each}}

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:


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


'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: [

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


stdob-- February 2016

You need access to parent's property imagesFolder:

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

Post Status

Asked in February 2016
Viewed 1,443 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life