class TableNode(Node):
def __init__(self, cellvar, sequence, cols, cellnodes):
self.cellvar = cellvar
self.sequence = sequence
self.cols = cols
self.cellnodes = cellnodes
def __iter__(self):
for node in self.cellnodes:
yield node
def get_nodes_by_type(self, nodetype):
nodes = []
if isinstance(self, nodetype):
nodes.append(self)
nodes.extend(self.cellnodes.get_nodes_by_type(nodetype))
return nodes
def render(self, context):
nodelist = NodeList()
if context.has_key('parenttable'):
parenttable = context['parenttable']
else:
parenttable = {}
context.push()
try:
values = self.sequence.resolve(context, True)
except VariableDoesNotExist:
values = []
if values is None:
values = []
if not hasattr(values, '__len__'):
values = list(values)
len_values = len(values)
innernodelist = NodeList()
for i, item in enumerate(values):
loopctx = {
# shortcuts for current loop iteration number
'counter0': i,
'counter': i+1,
'rowcounter0': i / self.cols,
'rowcounter': (i/self.cols) + 1,
'even': (i % 2 == 0),
# boolean values designating first and last items
'firstrow': (i < self.cols),
'lastrow': (i > len_values - self.cols),
'firstcell': (i == 0),
'lastcell': (i == len_values - 1),
'parenttable': parenttable,
}
context[self.cellvar] = item
loopctx["startrow"] = False
loopctx["endrow"] = False
if i % self.cols == 0:
nodelist.append(innernodelist.render(context))
innernodelist = NodeList()
loopctx["startrow"] = True
elif (i + 1) % self.cols == 0:
loopctx["endrow"] = True
context['table'] = loopctx
for node in self.cellnodes:
innernodelist.append(node.render(context))
if innernodelist != None and len(innernodelist) > 0:
nodelist.append(innernodelist.render(context))
print "Done rendering table"
context.pop()
return nodelist.render(context)
def do_table(parser,token):
bits = token.contents.split()
cellvar = bits[1]
sequence = parser.compile_filter(bits[2])
cols = int(bits[3])
cellnodes = parser.parse(('endtable',))
parser.delete_first_token()
return TableNode(cellvar, sequence, cols, cellnodes)
register.tag("table", do_table)
##Sample usage:
<table>
{% table pic gallery_pics 5 %}
{% if table.firstcell %}<tbody>{% endif %}
{% if table.startrow %}<tr>{% endif %}
<td>{{pic.filename}}</td>
{% if table.endrow %}</tr>{% endif %}
{% if table.lastcell %}</tbody>{% endif %}
{% endtable %}
</table>
Comments
Hey there,
I spent a little time getting this code integrated into my project - and thought I might share the changes I needed to perform to get it working - for all those copy-and-pasters (like myself).
I did have one question for anyone who might be able to answer it. I haven't spent much time modifying this code - however I noticed that it only creates as many td tags as you have elements in your sequence. For instance - if I have 6 elements and have set the maximum columns set to 5 - how do I get the second row to print those extra 4 td tags that are missing from the element? It doesn't seem to be affecting anything negatively right now - however I'm not sure it's valid.
Anyway, to get this code running:
create a templatetags directory in your application folder (not project)
Add an
__init__.pyfile in your templatetags directoryCreate a new .py file in the templatetags directory with the above code in it (comment out the html in the above code - after
##Sample usage:)Add the following to the top of the file:
from django import templateregister = template.Library()Change
Nodestotemplate.Nodesand all instances ofNodeList()totemplate.NodeList()In your django template page use
{% load imageTable %}where 'imageTable' is the name of the file you created in step oneNow everything should be solid
#
cannot make it work for me.
#
As a quick, simple way to make a table I added a custom template filter for modulo of an integer:
(add to .../mysite/myapp/templatetags/myapp_tags.py)
and then it's very easy in the template to make a table with n elements per row with a simple for loop by seeing if ( for_loop.counter modulo n ) is zero, and if so make a new table row:
#
See the update table tag for a couple more features...
http://www.djangosnippets.org/snippets/1542/
#