Wrapping HTML in templates without repeating yourself?

2 views
Skip to first unread message

AndyB

unread,
May 27, 2008, 12:07:06 PM5/27/08
to Django users
Hi,

I am curious to know what people do in the following kind of
situation:

{% if url %}<a href='{{url}}'>{% endif %}
<div class='something'>Some content</div>
{% if url %}</a>{% endif %}

Repeating the 'if' bugs me especially if the logic is more
complicated. I can think of solutions involving custom tags etc. but I
would like to tap the collective wisdom of the group before creating
my own nasty little hack and I'm sure there's something more elegant I
haven't thought of yet.

cheers,

Andy Baker

blis102

unread,
May 28, 2008, 2:11:59 PM5/28/08
to Django users
Actually that is what I've resorted to as well. If there is something
better Id love to hear it as well. There is one other way, but not any
more convenient.

{% if url %}
<a href='{{url}}'><div class='something'>Some content</div></a>
{% else %}
<div class='something'>Some content</div>
{% endif %}

But thats even more bloated, so afik the solution you have is best.

Hope there is something better out there...

Eric Wertman

unread,
May 28, 2008, 5:19:47 PM5/28/08
to django...@googlegroups.com
I'm sort of new here.. but couldn't you just put that chunk in a small
template file, and if block the template?
Reply all
Reply to author
Forward
0 new messages