[Twisted-web] [Nevow] template performance

L. Daniel Burr ldanielburr at mac.com
Fri Dec 15 12:11:29 CST 2006


On Fri, 15 Dec 2006 10:31:55 -0600, Manlio Perillo  
<manlio_perillo at libero.it> wrote:

> L. Daniel Burr ha scritto:
>> [...]
>>  Yes, I understand that too.  I will point out that I have *never* used
>> a data directive in any of my applications.  I'm not sure why you feel
>> that you need them.
>
> Because I want to write XHTML code as much as possible.
> This is possible with Djando and it is "fast", why it should not be  
> possible with Nevow?
>
>> The fact that they are going away at some point is
>> a very good thing, in my opinion.
>>
>
> If data directive are going away and I will no more be able to write:
>
>     <table>
>        <caption>RandomTable</caption>
>        <thead>
>          <tr n:render="sequence" n:data="header">
>            <th n:pattern="item" n:render="string" />
>          </tr>
>        </thead>
>        <tbody n:render="sequence" n:data="table">
>          <tr n:pattern="item" n:render="sequence">
>            <td n:pattern="item" n:render="string" />
>          </tr>
>        </tbody>
>      </table>
>

You can still have patterns, and you can still render tables.

> I will migrate to twisted web2 + django template, where I can write:
>
>      <table>
>        <caption>RandomTable</caption>
>        <thead>
>          <tr>
>            {% for i in header %}
>              <th>{{ i }}</li>
>            {% endfor %}
>          </tr>
>        </thead>
>        <tbody>
>          {% for i in table %}
>            <tr>
>              {% for j in i %}
>                <td>{{ j }}</td>
>              {% endfor %}
>            </tr>
>          {% endfor %}
>        </tbody>
>      </table>
>

I will note that this is most definitely *not* XHTML, so your
statement "...want to write XHTML code as much as possible."
doesn't make sense.  The nevow template would probably pass
XHTML validation, but this Django template surely won't, so
I don't understand your point.

Moreover, you've embedded iteration logic in the Django
template, so if some idea of "XHTML purity" is driving you,
the the Django approach, while faster, is less pure.

>
>
>
> However I want to be sure.
> Maybe I'm using Nevow in the wrong way.
>
> How do you render a table like the one in the example?
>

You could do something like this (untested):

template.xhtml:

<table xmlns="http://www.w3.org/1999/xhtml"
	xmlns:n="http://nevow.com/ns/nevow/0.1">

     <caption>RandomTable</caption>

     <thead>
         <tr n:render="headers">
             <th n:pattern="item">
                 <n:slot name="content"/>
             </th>
         </tr>
     </thead>

     <tfoot/>

     <tbody n:render="rows">
         <tr n:pattern="item">
             <td n:pattern="item">
                 <n:slot name="content"/>
             </td>
         </tr>
     </tbody>

</table>


 from nevow import flat, loaders, page


class MyTable(page.Element):
     docFactory = loaders.xmlfile('template.xhtml')

     def headers(self, request, tag):
         headerPattern = tag.patternGenerator('item')
         columns = []
         for i in range(5):
             header = headerPattern()
             header.fillSlots('content', i)
             columns.append(header)
         return tag.clear()[columns]

     def rows(self, request, tag):
         rowPattern = tag.patternGenerator('item')
         rows = []
         for i in range(10):
             row = rowPattern()
             cells = []
             cellPattern = row.patternGenerator('item')
             for j in range(5):
                 cell = cellPattern()
                 cell.fillSlots('content', j)
                 cells.append(cell)
             rows.append(row.clear()[cells])
         return tag.clear()[rows]

     page.renderer(headers, rows)


This is one possible way to do it, and it works now,
with the current nevow release.

Note that the template is generally similar to your
original one, and we've kept the looping out of the
template.

The most important thing is to stop thinking in terms
of data types (render="sequence", render="string"),
and just write render methods that deal with app-level
concepts (headers, rows).

An important point to make here, is that the code I
provided above is based on the same code used to
implement the "sequence" directive, so there is really
nothing magical going on.

>
>
>
> Thanks and regards  Manlio Perillo

Hope this helps,

L. Daniel Burr



More information about the Twisted-web mailing list