Well, I think my suggestion is obviously that when a template tag is in a loop, it should be re-initiated on every iteration, since that would avoid unexpected and non-obvious side effects.
-Scott On May 5, 2:52 pm, "burc...@gmail.com" <burc...@gmail.com> wrote: > Hi Apreche, > > I don't want to be rude, but you probably wanted to send this to > django-users, it is the place full of users of django framework who > should be aware of your problem and this point of confusion (hm, or > should it be blog post so everyone can google on their problem and > find your message?). > Everyone here in django-dev assumes that you have suggestion to fix > something in django if you're writing here. > Probably, you might suggest to improve django documentation where it > explains how to create custom template tags. > It seems you're really good at writing documentation! > Then, please, go on, improve the current documentation text, create a > ticket, and send your patch. > > > > On Wed, May 5, 2010 at 10:12 PM, Apreche <apre...@gmail.com> wrote: > > I had a really strange bug in one of my custom template tags today. It > > took me the entire morning to figure it out, but I finally did. I'm > > not sure if it's a bug or a feature, but I wanted to make a post to > > help the next person who has this same problem. > > > Let's pretend you make a custom template tag foo. The render method > > for foo is in a class FooNode, and you pass it a person from the > > context. > > > {% foo persona %} > > {% foo personb %} > > > Then, as is common, you have a list of people that you render with a > > loop in the template. > > > {% for person in people %} > > {% foo person %} > > {% endfor %} > > > It turns out that when a template tag stands alone, the __init__ of > > FooNode will be called each time the template tag appears. However, > > when the template tag is inside of a loop, the __init__ of the FooNode > > is only called once. That single instance of FooNode sticks around, > > and it's render method is called over and over as the loop goes > > around. > > > This doesn't seem like that big of a deal. However, my template tag > > was written in such a way that data members of FooNode were being set > > in the __init__ and reused in the render(). Because the initialization > > only happened the first time through the loop, the template tag always > > rendered as if it was for the first person in the list. Those data > > members carried over to subsequent renders because the Node was not > > reinitialized. I managed to fix it, but be aware. If your template > > tags are behaving strangely when they are in loops, this is probably > > the issue. > > -- > Best regards, Yuri V. Baburov, ICQ# 99934676, Skype: yuri.baburov, > MSN: bu...@live.com > > -- > You received this message because you are subscribed to the Google Groups > "Django developers" group. > To post to this group, send email to django-develop...@googlegroups.com. > To unsubscribe from this group, send email to > django-developers+unsubscr...@googlegroups.com. > For more options, visit this group > athttp://groups.google.com/group/django-developers?hl=en. -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.