Template range filter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django.template import Library

register = Library()

@register.filter
def get_range( value ):
  """
    Filter - returns a list containing range made from given value
    Usage (in template):

    <ul>{% for i in 3|get_range %}
      <li>{{ i }}. Do something</li>
    {% endfor %}</ul>

    Results with the HTML:
    <ul>
      <li>0. Do something</li>
      <li>1. Do something</li>
      <li>2. Do something</li>
    </ul>

    Instead of 3 one may use the variable set in the views
  """
  return range( value )

More like this

  1. range tag by marco.fucci 2 years, 8 months ago
  2. isoutc template filter by japerk 2 years, 10 months ago
  3. mkrange - create a range() inside a template - variable/filter support for range values by berserkpi 1 year, 5 months ago
  4. Template filter to convert timecodes into links by justin_h 2 years, 5 months ago
  5. Filter by first letter inclusion tag by slafs 1 year, 8 months ago

Comments

pro547 (on May 2, 2009):

This should be a default filter

#

marcalj (on July 28, 2009):

@pro547 I second that!

Thanks for the snippet! :)

#

jerry2801 (on October 20, 2009):

simple and great!

#

wolever (on February 14, 2010):

This may also be useful: http://www.djangosnippets.org/snippets/1926/ It lets you use the full power of range (ie, specifying a start, stop and step).

#

wolever (on February 14, 2010):

Err, that link should be: http://www.djangosnippets.org/snippets/1926/

#

rizumu (on October 19, 2010):

for one based, you can add 1 via a list comprehension

return [v + 1 for v in range(value)]

#

(Forgotten your password?)