kaplievabell kaplievabell - 4 months ago 39
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"
.name.left=person.fullName
else
.name.left=person.shortName


Thank you

Answer

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
else
   // 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"
  .name.left=person.fullName         
else
  .name.left=person.shortName

if type1 =="S"
  .name.left=person.fullName         
else
  .name.left=person.shortName

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