Switch/case conditional tags

  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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#-*- coding: utf-8 -*-
# Copyright (C) 2008 Alfaiati - Tecnologia sob medida
#
#   Author: Gabriel Falc√£o <gabriel.falcao@alfaiati.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
class SwitchNode(template.Node):
    def __init__(self, var1, nodelist_true, nodelist_false):
        self.comparison_base = Variable(var1)
        self.nodelist_true = nodelist_true
        self.nodelist_false = nodelist_false
        self.varname = var1
        
    def __repr__(self):
        return "<SwitchNode>"

    def render(self, context):
        try:
            val1 = self.comparison_base.resolve(context)
            bhash = "__%s__" % md5.new(self.varname).hexdigest()
            context[bhash] =  val1
        except VariableDoesNotExist:
            raise TemplateSyntaxError("Could not resolve variable %r in current context" % \
                                      self.comparison_base.var)
        ok = False
        for node in self.nodelist_true:
            if isinstance(node, CaseNode):
                node.set_source(bhash)
                if node.get_bool(context):
                    ok = True
        if ok:
            return self.nodelist_true.render(context)
        else:
            return self.nodelist_false.render(context)

@register.tag    
def switch(parser, token):
    """
    Create a context to use case-like coditional
    template rendering.

    For example::

        {% switch person.name %}
            {% case 'John Doe' %}
                Hi! My name is John, the master!
            {% endcase %}
            {% case 'Mary Jane' %}
                Hello! My name is Mary. Nice to meet you!
            {% endcase %}            
        {% default %}
            Oh my God! I have no name!
        {% endswitch %}
    """
    
    bits = list(token.split_contents())
    if len(bits) != 2:
        raise TemplateSyntaxError, "%r takes one argument" % bits[0]
    end_tag = 'end' + bits[0]
    nodelist_true = parser.parse(('default', end_tag,))
    token = parser.next_token()
    if token.contents == 'default':
        nodelist_false = parser.parse((end_tag,))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    return SwitchNode(bits[1], nodelist_true, nodelist_false)


class CaseNode(Node):
    def __init__(self, var, nodelist):
        self.var = Variable(var)
        self.nodelist = nodelist
        
    def __repr__(self):
        return "<CaseNode>"
    
    def set_source(self, var):
        """ Sets the varname to lookup in
        context and make the comparisons"""
        self.base_comparison = var
        
    def get_bool(self, context):
        try:
            val = self.var.resolve(context)
        except VariableDoesNotExist:
            val = None
            
        base_comparison = getattr(self, "base_comparison", None)
        if not base_comparison:
            raise LookupError("Could not find base_comparison. "
                              "Ensure to use {% case %} node "
                              "within a {% switch %} node")
        if context.get(self.base_comparison, None) == val:
            return True
        else:
            return False
        
    def render(self, context):
        if self.get_bool(context):
            return self.nodelist.render(context)
        else:
            return NodeList().render(context)


@register.tag    
def case(parser, token):
    bits = list(token.split_contents())
    if len(bits) != 2:
        raise TemplateSyntaxError, "%r takes one argument" % bits[0]
    end_tag = 'end' + bits[0]
    nodelist_true = parser.parse(('else', end_tag))
    token = parser.next_token()
    if token.contents == 'else':
        nodelist_false = parser.parse((end_tag,))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    return CaseNode(bits[1], nodelist_true)

More like this

  1. Easy Conditional Template Tags by fragsworth 4 years, 11 months ago
  2. Switch/case tags. by jacobian 6 years, 9 months ago
  3. Switch template tag by adurdin 5 years, 8 months ago
  4. Template range filter by zalun 5 years, 1 month ago
  5. tag to store a settings value as template variable by pflanno 2 years, 9 months ago

Comments

ericflo (on July 12, 2008):

I really like this snippet! Can see using it a lot.

#

(Forgotten your password?)