kaplievabell kaplievabell - 1 year ago 224
Jade Question

Use Lowercase in Jade conditional Statement

I wonder how to lowercase the value in the conditional statement within Jade template. The code below produces error:

if type.toUpperCase() =="S"

Thank you

Answer Source

Using functions in Jade/Pug conditionals doesn't work. It should always be if myVar == 'myCondition'

If you want to use functions or other stuff in an jade/pug conditional you should prepare your variables first. By leading new lines with a minus - you are able to write regular JavaScript in JADE / PUG. This gives you a powerfull weapon to resolve almost everything. For example you want to check an uppercased string against your condition you should do it like this:

- var string = 'myString'
- var myUppercaseString = string.toUpperCase()
if myUppercaseString == 'myCondition'
   // Do stuff if condition is met
   // Do stuff if condition isn't met

In your case it should look something like this:

-  var person = {
-    fullName: 'Hans Dieter Müller',
-    shortName: 'Hansi'
-  }
-  var type = 's'
-  var type1 = type.toUpperCase()

if type =="S"

if type1 =="S"

Here is a working example: http://codepen.io/pure180/pen/oLdVzg

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download