Saturday, March 29, 2014

Nesting custom template tags


I am trying to create a custom template tag that generates a repetitive HTML snippet. This tag is an anchor tag "a" with extra classes and styles, plus some logic to apply additional classes and styles. I want the syntax in the template to look something like:

{% my_anchor URL %}this is text between opening and closing tags{% end_my_anchor %}

Where URL is set to the href attribute of the anchor tag.

The rendered result should look something like:

<a class="myclass1 myclass2" style="mystyle1; mystyle2" href="URL">this is text between opening and closing tags</a>

I also do not want to hard code my URLs. So as one might expect, I am using the "url" template tag to create correct URLs. Naively, I tried to nest the tags:

{% my_anchor {% url ... %} %}this is text between opening and closing tags{% end_my_anchor %}

But this is invalid syntax. Is there a way to handle this?


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment