[Twisted-web] Rendering of list/table data with a template

Glyph Lefkowitz glyph at twistedmatrix.com
Tue Aug 23 14:26:19 EDT 2011

On Aug 23, 2011, at 8:34 AM, Florian Lindner wrote:

> Hello,
> 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:
>    <table>
>      <tr t:render="table_row"><td><t:slot name="table_content" /></td></tr>
>    </table>
> with that renderer:
>    @renderer
>    def table_row(self, request, tag):
>        rows = ["A", "B", "C"]
>        output_list = []
>        for row in rows:
>            t = tag.clone()
>            t.fillSlots(table_content=row)
>            output_list.append(t)
>        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:
        yield tag.clone().fillSlots(table_content=row)

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:

<table t:render="hasRows">
<tr t:render="table_row"><td><t:slot name="table_content" /></td></tr>

<div t:render="noRows">
No rows.

The renderers for these just look like this:

def hasRows(self, request, tag):
    rows = ...
    if not rows:
        return ''
    return tag

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...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20110823/31340489/attachment.htm 

More information about the Twisted-web mailing list