JimT wrote:
> I've searched pretty much everywhere and I still can't get my simple
> template tag working.
>
> It's supposed to simply return a list of strings which I can output in
> a template for loop. Currently it takes no parameters but eventually
> I'd like to pass it a string to format the URL as well however it
> doesn't seem to be rendering. Here's the tag code:
>
> import datetime
> from django.template import Library, Node
>
> register = Library()
>
> class DaysListNode(Node):
>       def render(self, context):
>               today = datetime.date.today()
>               days_list = []
>               for i in range(7):
>                       day_delta = datetime.timedelta(days=i)
>                       the_date = today + day_delta
>
>                       # Locale's abbreviated weekday name.
>                       day_name = the_date.strftime("%a")
>
>                       # Day of the month as a decimal number [01,31]
>                       day_num = the_date.strftime("%d").lstrip("0")
>
>                       days_list.insert(i, day_name + '<br />' + day_num)
>
>               context['days_list'] = days_list
>               return ''
>
> def calendar_days(parser, token):
>       return DaysListNode()
>
> register.tag('calendar_days', calendar_days)
>
>
> And here's the way I'm calling it in the template:
>
> {% load calendar_days %}
> ... lots of XHTML
>
> <ul class="date">
>       {% for day in calendar_days.days_list %}
>       <li class="{% if forloop.first %}active{% endif %}"><a href="#">
> {{ day }}</a></li>
>       {% endfor %}
> </ul>
>
> It doesn't seem to work if I put {% for day in calendar_days %} or {%
> for day in days_list %} either.
>
> Any help would be appreciated,
>
> Thanks

You're not calling the template tag anywhere. The way you've written
it, it inserts a variable called 'days_list' into the context. But you
actually have to call the tag first:
{% calendar_days %}

Then you can do
{% for day in days_list %}

One other point: you're manually escaping the HTML in the tag, for
some reason, which will actually result in it being escaped *twice*
when it's rendered. In fact you want to do the opposite - mark the
output as safe, so it doesn't get escaped and your <br/>s actually get
rendered as line breaks. You want this:
from django.utils.safestring import mark_safe
...
                        days_list.insert(i, mark_safe(day_name + '<br /
>' + day_num))
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to