On Fri, 2007-12-28 at 16:01 -0500, Ned Batchelder wrote: > I appreciate the advice, but forloop.first won't work for what I'm > trying to do. If the first thing chosen is in the second iteration of > the for loop, then forloop.first is never true. I don't want to know > if I'm on the first iteration of the loop. I want to know if it's the > first time in the if clause: > {% for thing in mylist %} > {% if thing.test %} > {% iffirst %}Things: {% else %}, {% endiffirst %} > {{thing}} > {% endif %} > {% endfor %}
Presumably, it's even more subtle than that. Don't you want to know if it's the first time in the "if" clause for this particular execution of the for-loop? In any case, change the syntax slightly. The "iffirst" tag should take a variable name, which is what will be set in the context to indicate if it's been executed before. So the render code for {% iffirst foo %} will check to see if context['foo'] is equal to id(context['forloop']). If not, set it to that value and return the "true" branch. Otherwise, return the "false" branch. Untested code follows: def render(self, context): if context[self.varname] == id(context['forloop']): return self.nodelist_false.render(context) context[self.varname] = id(context['forloop']) return self.nodelist_true.render(context0 Here. nodelist_true and nodelist_false could be similar to the same variables in django.template.defaulttags.IfNode (and do the setup the same way). I've been awake all night, so my brain is completely straight at the moment ... there might be some subtlety that means you need to use Variable.resolve() or something instead of just context[self.varname], but it should be clear with a bit of experimentation. You certainly won't need anything as complicated as what the IfNode class does, since you aren't testing a user-supplied condition. The only reason to use the variable name is so that you can have multiple "iffirst" calls in a single template without collisions. Clearly extending to allow another parameter that says which for-loop level of nesting ("parent", etc) would be another extension and not too difficult. Regards, Malcolm -- I just got lost in thought. It was unfamiliar territory. http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---