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:

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

Answers


stdob-- February 2016

You need access to parent's property imagesFolder:

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

Post Status

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

Search




Leave an answer