Login

If modulo template tag

Author:
voidberg
Posted:
August 21, 2007
Language:
Python
Version:
.96
Tags:
template tag if modulo
Score:
-2 (after 4 ratings)

Usage: {% ifmodulo forloop.counter 4 0 %} <!-- do something --> {% else %} <!-- do something else --> {% endifmodulo %}

or

{% ifnotmodulo 5 3 %} <!-- do something --> {% else %} <!-- do something else --> {% endifmodulo %}

When the third parameter does not exist, it is assumed you're checking for values different than 0.

 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
from django import template

register = template.Library()

def do_ifmodulo(parser, token, negate):
  bits = list(token.split_contents())
  if not len(bits) in [3, 4]:
    raise template.TemplateSyntaxError, "%r takes two or three arguments" % 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 = template.NodeList()
  if len(bits) == 3:
    bits.extend([None])
  return IfModuloNode(bits[1], bits[2], bits[3], nodelist_true, nodelist_false, negate)
  
def ifmodulo(parser, token):
  return do_ifmodulo(parser, token, False)
  
def ifnotmodulo(parser, token):
  return do_ifmodulo(parser, token, True)
  
register.tag('ifmodulo', ifmodulo)
register.tag('ifnotmodulo', ifnotmodulo)

class IfModuloNode(template.Node):
  def __init__(self, var1, var2, var3, nodelist_true, nodelist_false, negate):
    self.var1, self.var2, self.var3 = var1, var2, var3
    self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
    self.negate = negate
    
  def __repr__(self):
    return "<IfModuloNode>"

  def render(self, context):
    try:
      val1 = template.resolve_variable(self.var1, context)
    except template.VariableDoesNotExist:
      val1 = None
    try:
      val2 = template.resolve_variable(self.var2, context)
    except template.VariableDoesNotExist:
      val2 = None
    if self.var3 <> None:
      try:
        val3 = template.resolve_variable(self.var3, context)
      except template.VariableDoesNotExist:
        val3 = None
    else:
      val3 = None
    
    if val3 == None:
      if (not self.negate and val1 % val2) or (self.negate and not val1 % val2):
        return self.nodelist_true.render(context)
      else:
        return self.nodelist_false.render(context)
    else:
      if (not self.negate and val1 % val2 == val3) or (self.negate and val1 % val2 <> val3):
        return self.nodelist_true.render(context)
      else:
        return self.nodelist_false.render(context)

More like this

  1. Pagination Alphabetically compatible with paginator_class by vascop 3 years, 4 months ago
  2. Digg-like pagination by SmileyChris 6 years, 3 months ago
  3. better paginator template tag by amitu 6 years, 10 months ago
  4. Decode HTML Template Tag by megamark16 3 years, 5 months ago
  5. AddThis Social Networking TemplateTag by yeago 6 years, 8 months ago

Comments

voidberg (on August 23, 2007):

No, thanks for pointing that out. Now I feel like an idiot. :)

#

3nt (on September 17, 2008):

Thanks for this. I found it useful as the basic tag didn't do what I wanted. I needed to write different HTML depending on whether it was the 1,5,9 etc or 2,6,10 etc time through the loop (to facilitate YUI css nesting)

#

Please login first before commenting.