On 8/3/06, skullvulture <[EMAIL PROTECTED]> wrote: > Is there anyway to declare variables within a template? I'm coming > from a Zope background where with dtml you have the dtml-let tag and > with ZPT you have tal:define. Django's templating seems so much cooler > than Zope's, but I'm missing a declare or define tag. > > The idea I want to be able to do is: > > {% declare nopastshows=0 %} > {% for show in object_list %} > {% if show.is_past_show %} > <h4>{{ show.show_date.month }}/{{ show.show_date.day }} {{ > show.show_title}} @ <a href="{{ show.location_url }}" target="new">{{ > show.location_name }}</a></h4> > {% else %} > {% nopastshows=1 %} > {% endif %} > {% endfor %} > > {% if nopastshows %} > <h4>No past show information available</h4> > {% endif %}
Nope, there's no way to declare variables within templates, and that's on purpose. (See http://www.djangoproject.com/documentation/design_philosophies/ for the rundown.) There are a couple of ways to solve your particular problem: * Calculate the nopastshows variable within your view, where the "business logic" belongs. If you're using a generic view, you won't be able to do this. * If you're not using any of the objects with is_past_show == False, then change your view so that object_list is only the objects with is_past_show==True. Then you can do this: {% if object_list %} (Iterate and display objects.) {% else %} No past show information available. {% endif %} * You can write a custom template tag, as you noted. Hope this helps, Adrian -- Adrian Holovaty holovaty.com | djangoproject.com --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---