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

  1. in_list filter by guychi 8 years ago
  2. match filter by gsf0 6 years ago
  3. Simple Mobile Support by bahoo 4 years, 11 months ago
  4. simple string formatting filter by gsf0 8 years ago
  5. Create new variables in templates by jmrbcu 7 years, 7 months ago

Comments

james_027 (on December 28, 2007):

wow, this should be in the django official filter

#

Please login first before commenting.