Handlebarsjs check if a string is equal to a value

后端 未结 13 807
挽巷
挽巷 2020-12-08 18:23

Is it possible in Handlebars to check if a string is equal to another value without registering a helper? I can\'t seem to find anything relevant to this in the Handlebars r

13条回答
  •  甜味超标
    2020-12-08 19:23

    I would just use helpers like this:

    Handlebars.registerHelper('ifeq', function (a, b, options) {
        if (a == b) { return options.fn(this); }
        return options.inverse(this);
    });
    
    Handlebars.registerHelper('ifnoteq', function (a, b, options) {
        if (a != b) { return options.fn(this); }
        return options.inverse(this);
    });
    

    Then in your code:

    {{#ifeq variable "string"}} 
        ... do this ... 
    {{/ifeq}}
    {{#ifnoteq variable "string"}} 
        ... do this ... 
    {{/ifnoteq}}
    

提交回复
热议问题