Handlebars.js: Logical operator helper

Mean stack 23 ก.ย. 2016

Code

var hbs = require('hbs');

hbs.registerHelper({
    eq: function (v1, v2) { return v1 === v2; },
    ne: function (v1, v2) { return v1 !== v2; },
    lt: function (v1, v2) { return v1 < v2; },
    gt: function (v1, v2) { return v1 > v2; },
    lte: function (v1, v2) { return v1 <= v2; },
    gte: function (v1, v2) { return v1 >= v2; },
    and: function (v1, v2) { return v1 && v2; },
    or: function (v1, v2) { return v1 || v2; }
});

Use

{{#if (or section1 section2)}}
.. content
{{/if}}

or 

{{#if (or 
        (eq section1 "foo")
        (ne section2 "bar"))}}
.. content
{{/if}}

referent: http://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.