0

In my gulpfile I'm defining a memberInfo array of objects to pass to the template:

var gulp = require('gulp');
var handlebars = require('gulp-compile-handlebars');
var rename = require('gulp-rename');
var handlebars_helpers = require('handlebars-helpers')(); 
//http://assemble.io/helpers/helpers-html.html
var _ = require('underscore');

gulp.task('default', function () {
    var templateData = {
        memberInfo: [
            {"member_email": "amy@email.com", "list_id": "2", "name": "obsession cologne 3 oz", "brand": "", "price": "55.99"},
            {"member_email": "amy@email.com", "list_id": "1",  "name": "red plaid skirt", "brand": "versace", "price": "55.99"},
            {"member_email": "amy@email.com", "list_id": "1", "name": "black ruffle shirt", "brand": "versace", "price": "47.99"},
        ]
    }

    options = {
        helpers: _.extend(handlebars_helpers) 
    }

    return gulp.src('template/email.handlebars')
        .pipe(handlebars(templateData, options))
        .pipe(rename('email.html'))
        .pipe(gulp.dest('output'));
}

What I want to do on my template is say "for each object in memberInfo, if the list_id is equal to 1, display these fields"

{{name}}
{{brand}}
{{price}}

What is the proper way of doing this? I'm brand new to gulp and handlebars and the explanations I've found don't seem to make any sense in this context. Handlebar-helpers has a nifty comparison operator {{#is }} so I wrote this code, but it obviously isn't set up properly:

<ul>
  {{#each memberInfo}}
    {{#each this}}
        <li>Key: {{@key}} Value = {{this}}</li>
        {{#is @key 'list_id'  }} 
            {{#is this 1 }}
               {{name}}
               {{brand}}
               {{price}}
               //Nothing is accessible here except this, as far as i can tell, so none of these values render

            {{/is}}
        {{/is}}

    {{/each}}
  {{/each}}
</ul>
4

1 回答 1

0

我为这个半途而废的评论道歉,但我现在没有时间完全回答它。我有类似的情况,但我希望获得匹配并渲染它的项目数量。这是车把助手,我希望这个想法有所帮助。如果没有,请告诉我,我可以很快添加更多上下文。

exports.filterLength = function (object, property, value, options) {
  var ret = 0;

  for (var i = 0, j = object.length; i < j; i++) {
    if (object[i][property] === value) {
      ret = ret + 1;
    }
  }
  return ret;    
};
于 2017-07-14T19:58:19.000 回答