Jade conditional (if/else) to add class to div inline
Asked Answered
A

6

82

Is there a way to do this inline in a jade template?

if(typeof fromEdit != 'undefined')
   div#demo.collapse.in
else
   div#demo.collapse

Would like to do this conditional check "inline" and the result would add the .in to the end of the div if fromEdit exists.

Aeronaut answered 3/1, 2013 at 17:45 Comment(1)
i use this a(class=(selectNav=='myprofile' && 'selected'), href='...')Hassler
H
117

This works:

div#demo.collapse(class=typeof fromEdit === "undefined" ? "" : "in")

Try it out here.

Hau answered 3/1, 2013 at 17:48 Comment(2)
but you wouldn't even want the element to have a class attribute if the condition is false..Alard
How to do it for object value like a(class=tasks[i].status ? 'btn btn-success' : 'btn btn-dark', href=/turn/ ${tasks[i]._id}) Done showing error Cannot read property 'status' of undefinedUnmuzzle
B
83

If you don't want the class attribute to be added when there is no value, you can assign it undefined instead of an empty string. Here is the previous example, slightly modified:

div#demo.collapse(class=typeof fromEdit === "undefined" ? undefined : "in")

Update: Also, if you are using pug, you can now add as many class= declarations as you want with different conditions and they'll get concatenated in the resulting class attribute. e.g.:

#demo.collapse(class=cond1 && 'class1' class=cond2 && 'class2')
Barcroft answered 9/6, 2013 at 22:34 Comment(1)
What about a case when there are multiple condition must be checked? For example two classes each with its own check?Armin
C
6

As documented at http://jade-lang.com/reference/attributes/:

The class attribute [...] It can also be an object mapping class names to true or false values, which is useful for applying conditional classes

the task can be also done by the following:

div#demo.collapse(class={ in: typeof fromEdit != 'undefined' })

Although it doesn't work here http://naltatis.github.com/jade-syntax-docs/ (I think they need to update something), but it works with [email protected] .

Commodity answered 15/5, 2016 at 14:6 Comment(1)
This is the way to go now with jade/pug. It allows multiple conditionnal classes, with adding another key to the object.Jiggle
X
3

With pug 2 you can use this syntax:

div#demo(class="collapse", class={"in": typeof fromEdit !== 'undefined'}) Home page

more here: https://pugjs.org/language/attributes.html

Xe answered 25/5, 2017 at 23:6 Comment(0)
V
2

Though an old question, I find that the following works since Pug includes object existence detection built in:

div#demo.collapse(class=fromEdit? 'in':undefined)

If it's not obvious, this checks if fromEdit exists and if it does enters in as the class, otherwise leaving the class blank.

Vassalage answered 13/11, 2016 at 9:27 Comment(0)
S
1

Multiple conditional classes

p(class={"true-class": isTrue, "false-class": !isTrue})
Strike answered 7/4, 2022 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.