Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Handlebars ternary helper?

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?

like image 356
ryanve Avatar asked Apr 03 '15 23:04

ryanve


People also ask

How do I register Handlebars in Helper?

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.

What are helpers in handlebar?

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).

What is the use of Handlebars?

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.


1 Answers

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>
like image 67
doowb Avatar answered Oct 18 '22 03:10

doowb