sublist

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

@register.filter
def sublist(value, arg):
    """
    Usage: list|sublist:"a:b"
    Returns list[a:b]
    Accepts ":b" and "a:" shortcuts
    Note that the double quotes are necessary
    """
    try:
        arg = str(arg)
        mylist = list(value)
    except (ValueError, TypeError):
        return value
    if arg.find(':') < 0:
        return value
    a, b = arg.split(':')[:2]
    if a == "":
        a = None
    else:
        try:
            a = int(a)
        except ValueError:
            return value
    if b == "":
        b = None
    else:
        try:
            b = int(b)
        except ValueError:
            return value
    return mylist[a:b]

More like this

  1. Display arbitrary models by bjornkri 5 years, 9 months ago
  2. Calendar table by fauxparse 5 years, 11 months ago
  3. Tablify templatetag by davidblewett 6 years, 10 months ago
  4. django-noserun for testing by mjt 5 years ago
  5. Template tag: split list to n sublists by movielady 5 years, 9 months ago

Comments

nofactor (on November 21, 2007):

ok, i'm reinventing the wheel! is it possible to remobe a snippet?

#

nofactor (on November 21, 2007):

To admins: Please remove this snippet

#

(Forgotten your password?)