How do I lowercase a field using Handlebars.js?

前端 未结 5 1082
我寻月下人不归
我寻月下人不归 2021-02-01 14:27

I want to do something like this:

{{user.name.toLowerCase()}}

but I get this error:

Error: Parse error on line X:
...tatus {{us         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 14:30

    Doesn't hurt to also check and make sure it is a string and if not return nothing.

    Handlebars.registerHelper('lowercase', function (str) {
      if(str && typeof str === "string") {
        return str.toLowerCase();
      }
      return '';
    });
    

    Usage :

    // now let's pass a string or variable to our helper
    {{lowercase 'MY NAME IS'}}
    

    Output :

    my name is
    

提交回复
热议问题