from django.template import Library, TemplateSyntaxError

@register.tag(name='dict')
def do_dict(parser, token):
    try:
        tag_name, varname, key = token.contents.split(None, 2)
    except ValueError:
        raise TemplateSyntaxError("'dict' node requires a dict and a key name.")
    nodelist = parser.parse(('enddict',))
    parser.delete_first_token()
    return DictNode(nodelist, varname, key)

class DictNode(Node):
    def __init__(self, nodelist, varname, key):
        self.nodelist = nodelist
        self.varname, self.key = varname, key
        
    def render(self, context):
        output = self.nodelist.render(context)
        # get the top level context
        dict = context.dicts[len(context.dicts)-1]
        if self.varname in dict:
            dict[self.varname][self.key] = output
        else:
            dict[self.varname] = {self.key:output}
        return ''