A dict template tag

When you need to include a specific javascript file/code snippet in your page, it's always better to do it at the bottom of your page to avoid to block the rendering too soon. This tag provide you a nice way to include and launch only what is needed:

fromdjango.templateimportLibrary,TemplateSyntaxError@register.tag(name='dict')defdo_dict(parser,token):try:tag_name,varname,key=token.contents.split(None,2)exceptValueError:raiseTemplateSyntaxError("'dict' node requires a dict and a key name.")nodelist=parser.parse(('enddict',))parser.delete_first_token()returnDictNode(nodelist,varname,key)classDictNode(Node):def__init__(self,nodelist,varname,key):self.nodelist=nodelistself.varname,self.key=varname,keydefrender(self,context):output=self.nodelist.render(context)# get the top level contextdict=context.dicts[len(context.dicts)-1]ifself.varnameindict:dict[self.varname][self.key]=outputelse:dict[self.varname]={self.key:output}return''