Code layout change.
[wolnelektury.git] / src / wolnelektury / templatetags / switch_tag.py
1 # Source: http://djangosnippets.org/snippets/967/
2 # Author: adurdin
3 # Posted: August 13, 2008
4 #
5 #
6 # We can use it based on djangosnippets Terms of Service:
7 # (http://djangosnippets.org/about/tos/)
8 #
9 # 2. That you grant any third party who sees the code you post
10 # a royalty-free, non-exclusive license to copy and distribute that code
11 # and to make and distribute derivative works based on that code. You may
12 # include license terms in snippets you post, if you wish to use
13 # a particular license (such as the BSD license or GNU GPL), but that
14 # license must permit royalty-free copying, distribution and modification
15 # of the code to which it is applied.
16
17 from django import template
18 from django.template import Library, Node, VariableDoesNotExist
19
20 register = Library()
21
22
23 @register.tag(name="switch")
24 def do_switch(parser, token):
25     """
26     The ``{% switch %}`` tag compares a variable against one or more values in
27     ``{% case %}`` tags, and outputs the contents of the matching block.  An
28     optional ``{% else %}`` tag sets off the default output if no matches
29     could be found::
30
31         {% switch result_count %}
32             {% case 0 %}
33                 There are no search results.
34             {% case 1 %}
35                 There is one search result.
36             {% else %}
37                 Jackpot! Your search found {{ result_count }} results.
38         {% endswitch %}
39
40     Each ``{% case %}`` tag can take multiple values to compare the variable
41     against::
42
43         {% switch username %}
44             {% case "Jim" "Bob" "Joe" %}
45                 Me old mate {{ username }}! How ya doin?
46             {% else %}
47                 Hello {{ username }}
48         {% endswitch %}
49     """
50     bits = token.contents.split()
51     tag_name = bits[0]
52     if len(bits) != 2:
53         raise template.TemplateSyntaxError("'%s' tag requires one argument" % tag_name)
54     variable = parser.compile_filter(bits[1])
55
56     class BlockTagList(object):
57         # This is a bit of a hack, as it embeds knowledge of the behaviour
58         # of Parser.parse() relating to the "parse_until" argument.
59         def __init__(self, *names):
60             self.names = set(names)
61         def __contains__(self, token_contents):
62             name = token_contents.split()[0]
63             return name in self.names
64
65     # Skip over everything before the first {% case %} tag
66     parser.parse(BlockTagList('case', 'endswitch'))
67
68     cases = []
69     token = parser.next_token()
70     got_case = False
71     got_else = False
72     while token.contents != 'endswitch':
73         nodelist = parser.parse(BlockTagList('case', 'else', 'endswitch'))
74
75         if got_else:
76             raise template.TemplateSyntaxError("'else' must be last tag in '%s'." % tag_name)
77
78         contents = token.contents.split()
79         token_name, token_args = contents[0], contents[1:]
80
81         if token_name == 'case':
82             tests = map(parser.compile_filter, token_args)
83             case = (tests, nodelist)
84             got_case = True
85         else:
86             # The {% else %} tag
87             case = (None, nodelist)
88             got_else = True
89         cases.append(case)
90         token = parser.next_token()
91
92     if not got_case:
93         raise template.TemplateSyntaxError("'%s' must have at least one 'case'." % tag_name)
94
95     return SwitchNode(variable, cases)
96
97 class SwitchNode(Node):
98     def __init__(self, variable, cases):
99         self.variable = variable
100         self.cases = cases
101
102     def __repr__(self):
103         return "<Switch node>"
104
105     def __iter__(self):
106         for tests, nodelist in self.cases:
107             for node in nodelist:
108                 yield node
109
110     def get_nodes_by_type(self, nodetype):
111         nodes = []
112         if isinstance(self, nodetype):
113             nodes.append(self)
114         for tests, nodelist in self.cases:
115             nodes.extend(nodelist.get_nodes_by_type(nodetype))
116         return nodes
117
118     def render(self, context):
119         try:
120             value_missing = False
121             value = self.variable.resolve(context, True)
122         except VariableDoesNotExist:
123             no_value = True
124             value_missing = None
125
126         for tests, nodelist in self.cases:
127             if tests is None:
128                 return nodelist.render(context)
129             elif not value_missing:
130                 for test in tests:
131                     test_value = test.resolve(context, True)
132                     if value == test_value:
133                         return nodelist.render(context)
134         else:
135             return ""