HamlPy icon indicating copy to clipboard operation
HamlPy copied to clipboard

Django if-else tags common inner content

Open l0kix2 opened this issue 11 years ago • 1 comments

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?

l0kix2 avatar Apr 05 '14 12:04 l0kix2

Try using the yesno filter:

%div{ class: "{{ condition|yesno"active,inactive" }}" }
%img{'src': '={ image_obj.url }'}

davidtingsu avatar Jul 07 '14 08:07 davidtingsu