Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 840 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Jade conditional (if/else) to add class to div inline

#1
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.
Reply

#2
This works:

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

Try it out [here](

[To see links please register here]

).
Reply

#3
As documented at

[To see links please register here]

:

> 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

[To see links please register here]

(I think they need to update something), but it works with [email protected] .
Reply

#4
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.
Reply

#5
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](

[To see links please register here]

), 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')
Reply

#6
With pug 2 you can use this syntax:

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

more here:

[To see links please register here]

Reply

#7
Multiple conditional classes

```
p(class={"true-class": isTrue, "false-class": !isTrue})
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through