shannonmoeller / gulp-hb

A sane Gulp plugin to compile Handlebars templates. Useful as a static site generator.
http://npm.im/gulp-hb
MIT License
147 stars 14 forks source link

How to access file data in ver 3.0 #35

Closed thecreation closed 8 years ago

thecreation commented 8 years ago

Hi there,

In the v2.6.5, we can use options.data.root.file to access the file object on helper register callaback. But in the new versions, it throw a error that file is not undefined.

(function() {
  module.exports.register = function(Handlebars) {
    var path = require('path');

    Handlebars.registerHelper("file", function(options) {
      var file = options.data.root.file;

      return file.path;
    });
  };
}).call(this);
shannonmoeller commented 8 years ago

You need to specify the file flag which is now false by default.

gulp.src('...')
    .pipe(hb({
        file: true
    }))
    .pipe(gulp.dest('...'))
shannonmoeller commented 8 years ago

@amazingSurge You inspired a change. I dropped the { file: true } flag and added a @file property. In your example you'll access it like this:

    Handlebars.registerHelper("file", function(options) {
        var file = options.data.file;

        return file.path;
    });

In a template you'd access it like this (which does the same thing as your helper):

{{@file.path}}

Release pending completion of CI tests.

shannonmoeller commented 8 years ago

Released as v5.0.0.

https://github.com/shannonmoeller/gulp-hb/tree/v5.0.0#-data-variables

thecreation commented 8 years ago

Thank you! It works.