[Twisted-web] Re: choosing between nodes

Alex Martelli aleaxit at yahoo.com
Tue Apr 13 01:35:48 MDT 2004

Alex Levy wrote:

> On Sat, 2004-04-10 at 18:27, Ellers wrote:
>> <div id="content">
>>  <span nevow:data="display" nevow:render="xml"/>
>>  <span nevow:data="edit" nevow:render="xml">
>>   <form>this is edit: more will go in here...</form>
>>  </span>
>>  <span nevow:data="save" nevow:render="xml/>
>>   <p>you have saved: <span nevow:data="saved_data" nevow:render="xml/>
>>  </span>
>> </div>
> First off, you don't want to be using span tags, you want to be using
> div tags -- span tags aren't block elements, so they aren't supposed to
> contain things like forms and paragraphs (as far as I understand).

Unless you really need some tag to be in the output, which is unusual for
span and div unless you're placing HTML attributes on them, you may prefer
to attach your "rendering logic" to <nevow:invisible> tags instead.  Those
won't show up in the resulting HTML, which makes it "lighter".

As I often need selective output, I have a very simple general-purpose

    def render_ifeq(self, context, data):
        if data != (context.tag.pattern or ''):
            return ''
        return context.tag

If you have, for example, a data_state that returns as the current data a
string which represents some kind of "state", you could code the template:

<nevow:invisible nevow:data="state">
    <nevow:invisible nevow:render="ifeq" nevow:pattern="edit">
        <p> You are editing the object. </p>
    <nevow:invisible nevow:render="ifeq" nevow:pattern="save">
        <p> Your changes have been saved. </p>
    <nevow:invisible nevow:render="ifeq" nevow:pattern="view">
        <p> Your are just viewing the object. </p>

This will emit a single one of the three <p>'s as appropriate.  Several
other similar approaches are possible, e.g., put the patterns on the tags
among which you want to choose, and have the renderer examine all contained
patterns and pick the right one[s]; generalize ifeq to an ifmatch that
treats patterns as regular expressions; and so on, and so forth.  I'm
striving to avoid obscuring my stuff with excessive and unwarranted
generality, and so far I've found the simple ifeq serves my purpose, so
that's what I'm currently using.

I _think_ (I could be wrong...) that using nevow:pattern as the "argument"
to be used by data and render methods is quite consonant to nevow's design;
there used to be some confusion, e.g. with the quondam nevow:key, but now
that nevow:pattern is all that's left the approach appears to be to be
simple and effective, allowing you to pick the right degree of generality.


More information about the Twisted-web mailing list