Login

dict recurse template tag for django

Author:
stefanp
Posted:
April 2, 2010
Language:
Python
Version:
1.1
Tags:
template templatetag json dict resurse
Score:
0 (after 0 ratings)

Based on http://www.djangosnippets.org/snippets/592/, a simplified recurse template tag that will explore dict and list objects. Useful and straightforward for JSON documents (for instance from couchdb :p).

Actual usage example:

{% recursedict mydictionary %} <ul> {% loop %} <li>{% if key %}<b>{{ key }}</b>:&nbsp;{% endif %}{% value %}</li> {% endloop %} </ul> {% endrecurse %}

The main tag syntax is "recursedict var" where var is your dictionary name.

The "key" property will contain the current key, or None if the current value comes from a list (in other words the list (a, b, c) is handled like a very hypothetical {None: a, None: b, None: c} dictionary.)

{% value %} will output the current value, or recurse if the value is a list, tuple or dict.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
###############################################################################
# Recurse dictionary template tag for Django v1.1
# Copyright (C) 2008 Lucas Murray, (C) 2010 Stefan Praszalowicz
# http://www.undefinedfire.com 
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
###############################################################################

from django import template

register = template.Library()

class RecurseDictNode(template.Node):
    def __init__(self, var, nodeList):
        self.var = var
        self.nodeList = nodeList

    def __repr__(self):
        return '<RecurseDictNode>'

    def renderCallback(self, context, vals, level):
        if len(vals) == 0:
            return ''

        output = []

        if 'loop' in self.nodeList:
            output.append(self.nodeList['loop'].render(context))

        for k, v in vals:
            context.push()
            
            context['level'] = level
            context['key'] = k
            
            if 'value' in self.nodeList:
                output.append(self.nodeList['value'].render(context))
                
                if type(v) == list or type(v) == tuple:
                    child_items = [ (None, x) for x in v ]
                    output.append(self.renderCallback(context, child_items, level + 1))
                else:
                    try:
                        child_items = v.items()
                        output.append(self.renderCallback(context, child_items, level + 1))
                    except:
                        output.append(unicode(v))
            
            if 'endloop' in self.nodeList:
                output.append(self.nodeList['endloop'].render(context))
            else:
                output.append(self.nodeList['endrecursedict'].render(context))
            
            context.pop()

        if 'endloop' in self.nodeList:
            output.append(self.nodeList['endrecursedict'].render(context))

        return ''.join(output)

    def render(self, context):
        vals = self.var.resolve(context).items()
        output = self.renderCallback(context, vals, 1)
        return output

def recursedict_tag(parser, token):
    bits = list(token.split_contents())
    if len(bits) != 2 and bits[0] != 'recursedict':
        raise template.TemplateSyntaxError, "Invalid tag syxtax expected '{% recursedict [dictVar] %}'"

    var = parser.compile_filter(bits[1])
    nodeList = {}
    while len(nodeList) < 4:
        temp = parser.parse(('value','loop','endloop','endrecursedict'))
        tag = parser.tokens[0].contents
        nodeList[tag] = temp
        parser.delete_first_token()
        if tag == 'endrecursedict':
            break

    return RecurseDictNode(var, nodeList)

recursedict_tag = register.tag('recursedict', recursedict_tag)

More like this

  1. keywords arguments parser for custom template tags by bruno 6 years, 3 months ago
  2. Recurse template tag for Django by Zarin 7 years, 2 months ago
  3. packjs templatetag by kyprizel 7 years, 4 months ago
  4. Template filter that divides a list into exact columns by davmuz 3 years, 3 months ago
  5. Active link by ronnie 3 years, 5 months ago

Comments

Please login first before commenting.