[Twisted-web] Rendering of list/table data with a template
glyph at twistedmatrix.com
Tue Aug 23 14:26:19 EDT 2011
On Aug 23, 2011, at 8:34 AM, Florian Lindner wrote:
> I'm still trying to figure out what is the correct way to render data in a
> template that comes from a list, e.g. to produce a table.
> I have figured out a way, but I got no idea if that's the way to go:
> <tr t:render="table_row"><td><t:slot name="table_content" /></td></tr>
> with that renderer:
> def table_row(self, request, tag):
> rows = ["A", "B", "C"]
> output_list = 
> for row in rows:
> t = tag.clone()
> return output_list
> Is that how it should be done in twisted templating?
This is pretty close to the way I've been doing it, which is:
def table_row(self, request, tag):
rows = ['A', 'B', 'C']
for row in rows:
The logic is basically the same, it is just a little bit shorter. Sometimes I will also include a 'hasRows' and 'noRows' renderers in the outer template, like:
<tr t:render="table_row"><td><t:slot name="table_content" /></td></tr>
The renderers for these just look like this:
def hasRows(self, request, tag):
rows = ...
if not rows:
Fans of nevow's 'pattern' feature will be quick to point out that this is one of the use-cases for patterns, and indeed, so far I have had to write some logic to cache 'rows' on 'self', and a few redundant renderers which would have been unnecessary if we had patterns. But I actually find templates like this easier to read, and it saves me from having to use <t:transparent>s that hold the table and the div in the same renderer, which makes my browser ever so slightly happier to view the template un-rendered. I think I'll probably add some utilities for doing these things automatically, once I have a little more experience with this new, simplified templating style :).
Hope this was a helpful insight into some real-world t.w.t usage :).
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Twisted-web