handlebars-loader icon indicating copy to clipboard operation
handlebars-loader copied to clipboard

knownHelpers not working or documentation unclear and therefore unusable

Open SKGrimes opened this issue 9 years ago • 3 comments

cheers for building a useful tool. The documentation for node doesn't seem extensive, so maybe I am misunderstanding the use case here, but knownHelpers is meant to only use a subset of a library and it takes the form Array.

With the following standard requirements:

var Handlebars =          require('handlebars');   
var HandlebarsIntl = require('handlebars-intl');   
HandlebarsIntl.registerWith(Handlebars); 

The application functions as expected, this library formats dates and currency if you are unfamiliar. Looking for a bit of optimization, and also interested in escaping some html, I checked the reference link.

Hash containing list of helpers that are known to exist

maybe it is the 24 hours no sleep (it is), that threw me off for a few minutes tring to figure out how a hash would make sense (e.g. knownHelpers : { ??? : 'helper1' }. Fairplay, but I figured I better check into it. Stack Overflow : 1 result for knownHelpers asking for help. Ok, Cannot use 'in' operator to search for '0' in formatDate, makes sense, it is an array.

var options = {
 assumeObjects : true,
 knownHelpersOnly : true
};

Returns: You specified knownHelpersOnly, but used the unknown helper formatDate - 30:39, which makes sense. So I am using formatDate, well I'll just add formatDate and be on my way. It seems to indicate that if you explicitly tell handlebars about the specific helper, it will work a bit faster and ignore others. Maybe this doesn't matter as I registered it already, but I figured I'd let you know.

var options = {
 assumeObjects : true,
 knownHelpers : ['formatDate']
 knownHelpersOnly : true,
};

errorYou specified knownHelpersOnly, but used the unknown helper formatDate - 30:39

Idk, a bit weird, maybe I am misunderstanding it a bit more. Awesome project, just have been having trouble doing a few "non-traditional" things. I used it as a template to build nodemailer emails and currently to generate PDF invoices. I searched though the repo, the top 3 hits for "knownHelpers" there was a test file and a few other things. There seem to be no results or examples of what that options obj does. Maybe it doesn't matter, is deprecated but a bit more color on the options object and just a very short example would be awesome.

SKGrimes avatar Nov 03 '15 13:11 SKGrimes

Hi @SKGrimes , I'm facing the same issue and unable to find a solution yet; Clue me in on what's happening with this.

Thanks

dkarandana avatar Jan 12 '18 05:01 dkarandana

This was opened a long time ago and there is another thread that suggests it was solved -- https://github.com/pcardune/handlebars-loader/pull/141 -- but as to how it works, no clue. The examples do not conform to Webpack 2 config settings, that's for sure.

davedub avatar Jan 28 '18 19:01 davedub

I had the same problem and was able to resolve it like this:

{
                test: /\.hbs$/,
                loader: 'handlebars-loader',
                exclude: /node_modules/,
                options: {
																					 
                precompileOptions: {
                        knownHelpers: ['reverseWord'],
                        knownHelpersOnly: false,
                    },
                },

            }

And then in my template.js:

import Home from '../templates/Home.hbs';
import Cards from '../collections/Cards.js';
import Handlebars from 'handlebars/runtime'
Handlebars.registerHelper('reverseWord', function(value) {
    var reversedWord = value.split("").reverse().join("");
    return reversedWord;
});

and finally in .hbs

{{[reverseWord] 'one'}}

I didn't see ANY answers using import so I hope this helps someone else in the future!

anikaabreu avatar Mar 15 '19 13:03 anikaabreu