1 # Source: http://djangosnippets.org/snippets/967/
3 # Posted: August 13, 2008
6 # We can use it based on djangosnippets Terms of Service:
7 # (http://djangosnippets.org/about/tos/)
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.
17 from django import template
18 from django.template import Library, Node, VariableDoesNotExist
23 @register.tag(name="switch")
24 def do_switch(parser, token):
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
31 {% switch result_count %}
33 There are no search results.
35 There is one search result.
37 Jackpot! Your search found {{ result_count }} results.
40 Each ``{% case %}`` tag can take multiple values to compare the variable
44 {% case "Jim" "Bob" "Joe" %}
45 Me old mate {{ username }}! How ya doin?
50 bits = token.contents.split()
53 raise template.TemplateSyntaxError("'%s' tag requires one argument" % tag_name)
54 variable = parser.compile_filter(bits[1])
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
65 # Skip over everything before the first {% case %} tag
66 parser.parse(BlockTagList('case', 'endswitch'))
69 token = parser.next_token()
72 while token.contents != 'endswitch':
73 nodelist = parser.parse(BlockTagList('case', 'else', 'endswitch'))
76 raise template.TemplateSyntaxError("'else' must be last tag in '%s'." % tag_name)
78 contents = token.contents.split()
79 token_name, token_args = contents[0], contents[1:]
81 if token_name == 'case':
82 tests = map(parser.compile_filter, token_args)
83 case = (tests, nodelist)
87 case = (None, nodelist)
90 token = parser.next_token()
93 raise template.TemplateSyntaxError("'%s' must have at least one 'case'." % tag_name)
95 return SwitchNode(variable, cases)
97 class SwitchNode(Node):
98 def __init__(self, variable, cases):
99 self.variable = variable
103 return "<Switch node>"
106 for tests, nodelist in self.cases:
107 for node in nodelist:
110 def get_nodes_by_type(self, nodetype):
112 if isinstance(self, nodetype):
114 for tests, nodelist in self.cases:
115 nodes.extend(nodelist.get_nodes_by_type(nodetype))
118 def render(self, context):
120 value_missing = False
121 value = self.variable.resolve(context, True)
122 except VariableDoesNotExist:
126 for tests, nodelist in self.cases:
128 return nodelist.render(context)
129 elif not value_missing:
131 test_value = test.resolve(context, True)
132 if value == test_value:
133 return nodelist.render(context)