Handlebarsjs check if a string is equal to a value

后端 未结 13 826
挽巷
挽巷 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:01

    In Handlebars, the parenthesis are used to invoke the first item listed as a function, using (optional) subsequent items as parameters. So, the syntax from Ember CAN be used without Ember, provided you can set the context yourself. For example:

        context.eq = function(param1, param2) {
            return param1 === param2;
        }
    
        context.notEq = function(param1, param2) {
            return param1 !== param2;
        }
    

    Once you do that, you can use the standard {{#if}} and {{#unless}} block operations:

    {{#if (eq someVar "someValue") }}
    

    Be careful of switching contexts with {{with}} or when using inline partials. You can lose track of your defined "eq" function. The guaranteed way to work, regardless of new contexts:

    {{#if (@root.eq someVar "someValue") }}
    

提交回复
热议问题