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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
No, thanks for pointing that out. Now I feel like an idiot. :)
#
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.