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

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


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

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


that's it

No comments: