Skip to content

Conversation

@MadLittleMods
Copy link

This allows you to reference helpers directly inside the handlebars template. The @ format syntax is the same as the require-hbs plugin from Guy Bedford.

Format:

{{@helpers/myhelper}}
{{#@helpers/myblockhelper}}...{{/helpers/myblockhelper}}

Added path map option(helpersAtPathMap: {}) to map a module name (such as "helpers") to a complete path making it easier to reference helpers.

Also added documentation on this new @ format.


Example taken from added documenation to readme.md:

Example: inline @/at helpers

src/hello.handlebars

<p>Hello {{firstName}}</p>
<p>HELLO! {{@helpers/capitals.helper firstName}}</p>
{{#@helpers/noop.helper}}
    <p>A block helper that invokes the block as though no helper existed.</p>
{{/helpers/noop}}
{{> footer}}

src/js/hbs-helpers/capitals.helper.js

module.exports = function(str) {
    return str.toUpperCase();
}

src/js/hbs-helpers/noop.helper.js

module.exports = function(options) {
    return options.fn(this);
}

gulpfile.js

var gulp = require('gulp');
var handlebars = require('gulp-compile-handlebars');
var rename = require('gulp-rename');

gulp.task('default', function () {
    var templateData = {
        firstName: 'Kaanon'
    },
    options = {
        ignorePartials: true,
        partials : {
            footer : '<footer>the end</footer>'
        },
        helpersAtPathMap: {
            'helpers': './src/js/hbs-helpers/'
        },
    }

    return gulp.src('src/hello.handlebars')
        .pipe(handlebars(templateData, options))
        .pipe(rename('hello.html'))
        .pipe(gulp.dest('dist'));
});

dist/hello.html

<p>Hello Kaanon</p>
<p>HELLO! KAANON</p>
<p>A block helper that invokes the block as though no helper existed.</p>
<footer>the end</footer>

Currently, in the current master branch of this project, the only way to batch helpers is to do something like this:

var glob = require("glob");
var parsePath = require('parse-filepath');
var compileHandlebars = require('gulp-compile-handlebars');

//...
.pipe(compileHandlebars({/*context*/}, {
    helpers : (function() {
        var helpers = {};
        // Get a list of all the helpers in the folder
        var helperFiles = glob.sync("./src/js/hbs-helpers/*.js", {});
        helperFiles.forEach(function(filePath, index, array) {
            var pathParts = parsePath(filePath);
            try {
                helpers[pathParts.name] = require(filePath);
            } catch(e) {

            }
        });

        //console.log(helpers);
        return helpers;
    })()
}))

Format: `{{@helpers/myhelper}}` and `{{#@helpers/myblockhelper}}` --
Added path map option to map a module name to complete path making it
easier to reference helpers. Add option: `helpersAtPathMap: {}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant