And the following ugly loop adds scripts as I hoped simple concatenation would:
def render(self, context): for i in range(0, len(self.scripts)): context['scripts'].insert(i, self.scripts[i]) return '' Thomas On May 18, 2:31 pm, Thomas Allen <thomasmal...@gmail.com> wrote: > I would like to be able to allow any template file to add its own > JavaScript. Seemed like an easy implementation: > > class AddScriptNode(Node): > def __init__(self, scripts=[]): > self.scripts = scripts > > def render(self, context): > context['scripts'] = self.scripts + context['scripts'] > return '' > > @register.tag > def add_scripts(parser, token): > bits = list(token.split_contents()) > if len(bits) > 1: > return AddScriptNode(bits[1:]) > else: > return AddScriptNode() > > {% add_scripts moo moo-more nativelib pnc %} > > Note that I add the existing scripts to the end of the added scripts > because templates seem to be evaluated bottom-to-top, meaning a simple > extend() call would cause the sub-template scripts to be at the > beginning of the list (no good). > > The trouble is that this code clobbers context['scripts'] each time > render() is called. I have no idea why that is, because if I replace > that line with: > > context['scripts'].extend(self.scripts) > > then the scripts are added to the end, and nothing is clobbered. The > same goes for the equivalent: > > context['scripts'] += self.scripts > > This makes no sense to me. What's going on here? > > Thomas > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group > athttp://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.