2011-09-27

Jinja template and if/else class replacement

I'd like to add CSS classes to a html tags based on certain "conditions".

in this case I'm using jinja macro


{%- macro ifElseBoolTemplate(variable, trueValue="", falseValue="") -%}
{%- if variable -%}{{ trueValue }}{%- else -%}{{ falseValue }}{%- endif %}
{%- endmacro %}


(macro saved at file : macro/formMacro.html)


{% import 'macro/formMacro.html' as formTag with context %}
...
<div class="item {{ formTag.ifElseBoolTemplate(item.isActive, "", "inactive") }}" >sample content</div>


that's it

No comments: