Login

Template filters utils

Author:
marinho
Posted:
November 15, 2007
Language:
Python
Version:
.96
Tags:
template-filters
Score:
2 (after 2 ratings)

Explanation:

That template filters is util for combine boolean-return conditions using template tag {% if %}.

Setup:

Insert the snippet into an_app/templatetags/myutils.py.

Use in template: {% load myutils %} and use filters as following:

  • {% if 10|is_multiple_of:5 %}
  • {% if 10|in_list:a_list_variable %}
  • {% if user.username|is_equal:a_variable %}
  • {% if user.username|is_not_equal:a_variable %}
  • {% if a_variable_|is_lt:5 %}
  • {% if a_variable_|is_lte:5 %}
  • {% if a_variable_|is_gt:5 %}
  • {% if a_variable_|is_gte:5 %}
 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
from django.template import Library

register = Library()

@register.filter
def multiple_of(value,arg):
    return value % arg == 0

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

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

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

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

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

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

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

More like this

  1. Date/time util template filters by marinho 7 years, 3 months ago
  2. Template filter that divides a list into exact columns by davmuz 3 years, 1 month ago
  3. Tags & filters for rendering search results by exogen 6 years, 11 months ago
  4. A simple rest template filter by marinho 7 years, 3 months ago
  5. typygmentdown by ubernostrum 7 years, 6 months ago

Comments

Please login first before commenting.