HamlPy
HamlPy copied to clipboard
Django if-else tags common inner content
It's hard to describe my problem in summary, so i do it in example
I want to get html with div with img inside. Sometimes I want to assign one class to div tag, sometimes another. So i end up django-hamlpy template like this:
- if condition
.active
%img{'src': '={ image_obj.url }'}
- else
.inactive
%img{'src': '={ image_obj.url }'}
But I don't like %img{'src': '={ image_obj.url }'} copypaste in both blocks. It wasn't a problem with simple django tags, because there is endif tag and indents means nothing, but now i can't see how to do it in my new template scheme. How can i write django-haml markup without repeating tag content?
Maybe one just can't do something like that using indent-sensitive language and i should fallback to simple django tags, i still can use them as-is, right?
Try using the yesno filter:
%div{ class: "{{ condition|yesno"active,inactive" }}" }
%img{'src': '={ image_obj.url }'}