Login

Basic logic filters

Author:
mikeivanov
Posted:
September 11, 2007
Language:
Python
Version:
.96
Tags:
template filter logic
Score:
8 (after 8 ratings)

Usage:

{% if item|IN:list %}
  The item is in the list.
{% endif %}

{% if customer.age|LE:18 %}
  Go play out here.
{% endif %}

{% if product.price|add:delivery_cost|GT:balance %}
  Insufficient funds.
{% endif %}

You've got the idea.

Special thanks to guychi.

 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
from django import template
register = template.Library()

@register.filter
def EQ(value,arg): return value == arg

@register.filter
def LT(value,arg): return value < arg

@register.filter
def GT(value,arg): return value > arg

@register.filter
def LE(value,arg): return value <= arg

@register.filter
def GE(value,arg): return value >= arg

@register.filter
def NE(value,arg): return value != arg

@register.filter
def IS(value,arg): return value is arg

@register.filter
def IN(value,arg): return value in arg

More like this

Comments

james_027 (on December 28, 2007):
<p>wow, this should be in the django official filter</p>

#

Please login first before commenting.