Following this answer, I wrote a helper like
module.exports.register = function (Handlebars) {
Handlebars.registerHelper('ternary', function(test, yes, no) {
return test ? yes : no;
});
};
I'm certain that the helper is loaded and being defined but can't figure out the syntax to use it. I tried using it like
<div>{{ternary(true, 'yes', 'no')}}</div>
but that gives an assemble build error
Warning: Parse error on line 10:
...<div>{{ternary(true, 'yes',
----------^
Expecting 'ID', 'DATA', got 'INVALID' Use --force to continue.
What is the proper syntax to use a helper like that?
Handlebars. registerHelper("if", function(conditional, options) { if (conditional) { return options. fn(this); } }); When writing a conditional, you will often want to make it possible for templates to provide a block of HTML that your helper should insert if the conditional evaluates to false.
Helpers are the proposed way to add custom logic to templates. You can write any helper and use it in a sub-expression. For example, in checking for initialization of a variable the built-in #if check might not be appropriate as it returns false for empty collections (see Utils. isEmpty).
The Handlebars. Handlebars. js is a Javascript library used to create reusable webpage templates. The templates are combination of HTML, text, and expressions. The expressions are included in the html document and surrounded by double curly braces.
Handlebars helpers: http://handlebarsjs.com/#helpers don't follow the JavaScript syntax in the templates. You can use them like this:
<div>{{ternary true "yes" "no"}}</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With