In a Django template, I'd like to add CSS classes to a DIV based on certain "conditions", for example:
<div class="pkg-buildinfo {% if v.release.version == pkg.b.release.version %}active{% else %}inactive{% endif %} {% if v.release.version == project.latest.version %}latest{% else %}notlatest{% endif %}">
(note that v
is a loop variable; the whole thing is inside a for
loop)
The above adds CSS classes "active" or "inactive" and "latest" or "notlatest" based on two conditions.
This is however hard to read and verbose. I discovered that the with
statement does not support assigning the value of expressions/conditions (as opposed to complex variables) which is a pity. Is there a better way to do this?