TemplateTag to Split a List into Uniform Chunks

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

class SplitListNode(Node):
    def __init__(self, list_string, chunk_size, new_list_name):
        self.list = list_string
        self.chunk_size = chunk_size
        self.new_list_name = new_list_name

    def split_seq(self, seq, size):
        """ Split up seq in pieces of size, from
        http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425044"""
        return [seq[i:i+size] for i in range(0, len(seq), size)]

    def render(self, context):
        context[self.new_list_name] = self.split_seq(context[self.list], int(self.chunk_size))
        return ''

def split_list(parser, token):
    """<% split_list list as new_list 5 %>"""
    bits = token.contents.split()
    if len(bits) != 5:
        raise TemplateSyntaxError, "split_list list as new_list 5"
    return SplitListNode(bits[1], bits[4], bits[3])
    
split_list = register.tag(split_list)

More like this

  1. table with n items per row using custom modulo tag by elgreengeeto 5 years, 4 months ago
  2. Chunks template filter by oggy 5 years, 6 months ago
  3. Group sequence into rows and columns for a TABLE by davidwtbuxton 3 years, 2 months ago
  4. If in list template tag by udfalkso 6 years, 9 months ago
  5. partition template filters by SmileyChris 6 years, 7 months ago

Comments

noufal (on August 2, 2010):

Useful but if I say something like

{% list_to_columns obj.list_item as lists 2 %}

it borks because while obj is there in the context, obj.list_item is not. I used a dirty trick to work around this. Perhaps it's useful to you as well

def render(self, context):
    if "." in self.list:
        obj, attrs = self.list.split(".",1)
        obj = context[obj]
        attrs = attrs.split(".")
        for i in attrs:
            obj = getattr(obj,i)
        self.list = obj
    else:
        self.list = context[self.list]
    context[self.new_list] = self.split_seq(self.list, int(self.cols))
    return ''

#

(Forgotten your password?)