Skip to content

Instantly share code, notes, and snippets.

@impankratov
Last active September 8, 2021 15:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save impankratov/18e862e9a54d2ce092138e1a7a052c12 to your computer and use it in GitHub Desktop.
Save impankratov/18e862e9a54d2ce092138e1a7a052c12 to your computer and use it in GitHub Desktop.
Example of registering custom handlebars helpers (+ handlebars-form-helpers) with webpack
// Import Handlebars runtime lib
const Handlebars = require('handlebars/runtime');
const register = require('handlebars-form-helpers').register;
const { registerHandlebarHelpers } = require('../../some/other/place');
// Register extra helpers
register(Handlebars);
registerHandlebarHelpers(Handlebars);
/**
* Handlebars runtime with custom helpers.
* Used by handlebars-loader.
*/
module.exports = Handlebars;
// Don't forget to add the rest of the webpack configuration!
module.exports = {
module: {
rules: [
{
test: /\.hbs$/,
use: [
{
loader: 'handlebars-loader',
options: {
// Path to your custom js file, which has Handlebars with custom helpers registered
runtime: join(__dirname, 'app/src/handlebars')
}
}
]
}
]
},
resolve: {
extensions: ['.hbs']
}
};
@baloe
Copy link

baloe commented Jan 27, 2019

Could you please elaborate on how to make this work?
I integrated these snippets into my webpack project and I can build my project successfully, as long as I don't use my helper.
When inserting {{{concat 'a' 'b'}}} into my index.hbs I get errors though.

This is my handlebars.js:

const Handlebars = require('handlebars/runtime');

Handlebars.registerHelper('concat', function() {
            var outStr = '';
            for(var arg in arguments){
                if(typeof arguments[arg]!='object'){
                    outStr += arguments[arg];
                }
            }
            return outStr;
        } );

module.exports = Handlebars;

@v3nt
Copy link

v3nt commented Nov 3, 2020

yes a structure diagram would be great here!

@estebangarviso
Copy link

estebangarviso commented Sep 8, 2021

I had to add options.precompileOptions.knownHelpersOnly = false to handlebars-loader on webpack config in order to compile successfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment