You might want to look at the code for making a tag like:

URLNode(Node)

line 408 of

https://github.com/django/django/blob/stable/1.6.x/django/template/defaulttags.py

Then extend that.

class MyAnchor(URLNode):

override the init function to accept the params your are sending and 
override render so that it returns the HTML that you want.

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

would yield

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

K


On Saturday, March 29, 2014 8:24:16 AM UTC-7, Jon Dufresne wrote:
>
> Hi,
>
> 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?
>
> Cheers
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c28b85a4-fc5a-4a03-8f4d-d10588177395%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to