How to use helpers in handlebars with webpack(handlebars-loader)
Asked Answered
C

3

7

I am using Handlebars in my project and bundling templates using webpack. I am using handlebars-loader to compile templates. I got issue when I created a small helper. Webpack shows this error when I use helper in my template:

You specified knownHelpersOnly, but used the unknown helper withCurrentItem - 5:4

This is my code:

Webapck:

{
        test   : /\.(tpl|hbs)$/,
        loader : "handlebars-loader?helperDirs[]=" + __dirname + "templates/helpers"
        // use    : 'handlebars-loader?helperDirs[]=false' + __dirname + 'templates/helpers'
},

Helper(project/templates/helpers/withCurrentItem.js):

export default function (context, options) {
  const contextWithCurrentItem = context

  contextWithCurrentItem.currentItem = options.hash.currentItem

  return options.fn(contextWithCurrentItem)
}

Template file(project/templates/products.tpl):

{{> partials/filters}}
<ul class="u-4-5">
  {{#each data.products}}
    {{> partials/product}}
    {{withCurrentItem ../styles currentItem=this}}
  {{/each}}
</ul>

I tried to resolve the problem and searched over the internet but I couldn't find any thing. This is what I have tried to:

  • Add helperDirs[] query param to loader as:

    loader : "handlebars-loader?helperDirs[]=" + __dirname + "templates/helpers"

  • Add helpers directory path to resolve.modules property of webpack config file

Sadly, none of them work.

Clementclementas answered 14/6, 2017 at 11:25 Comment(0)
A
8

For me, none of these approaches worked. I used runtime option to create my own instance of Handlebars (thanks to this comment):

webpack.config.js

module: {
  rules: [
    {
      test: /\.(handlebars|hbs)$/,
      loader: 'handlebars-loader',
      options: {
        runtime: path.resolve(__dirname, 'path/to/handlebars'),
      },
    },

path/to/handlebars.js

const Handlebars = require('handlebars/runtime');
Handlebars.registerHelper('loud', function(aString) {
  return aString.toUpperCase();
});
module.exports = Handlebars;
Artel answered 5/4, 2020 at 17:29 Comment(0)
F
6

[email protected] and [email protected]:

{
  test: /\.hbs$/,
  loader: 'handlebars-loader',
  options: {
    helperDirs: path.join(__dirname, 'path/to/helpers'),
    precompileOptions: {
      knownHelpersOnly: false,
    },
  },
},

Update 2021: also works with webpack@4+.

Futhark answered 7/2, 2018 at 15:58 Comment(2)
Is there any way to register helpers without separate files for them in native hbs way (handlebars.registerhelper(args)) in webpack.config? Also how to register helpers from npm modules(handlebars-layouts, handlebars-helpers)?Ppi
If you're using webpack, you should avoid using inline helper (native hbs registerHelper), it's inconsistent. For other question, try to use config.helperResolver configuration for handlebars-loaderFuthark
E
1

Following config worked for me in webpack 4

// webpack
{
    test: /\.hbs$/,
    use: [{
        loader: 'handlebars-loader?runtime=handlebars/runtime',
        options: {
            precompileOptions: {
                knownHelpersOnly: false,
            }
        }
    }]
}


// helpers/ifEq.js
module.exports = function (a, b, opts) {
    if (a === b) {
        return opts.fn(this);
    }
    return opts.inverse(this);
}
Exaggerative answered 4/7, 2019 at 9:54 Comment(2)
Could you please share the code of helpers? I am doing exact same things but still helpers are not loading.Centenary
@PushkarKathuria Updated configurationExaggerative

© 2022 - 2024 — McMap. All rights reserved.