[Twisted-web] Nevow Templating question
indigo at bitglue.com
indigo at bitglue.com
Tue Apr 13 02:54:45 MDT 2004
Another option:
<select n:render='sequence' n:data='cityList'>
<option n:render='city' n:pattern='item'>
<n:attr name='value'><n:slot name='value'/></n:attr>
<n:slot name='city'/>
</option>
</select>
def data_cityList( self, context, data ):
return [("New York",'1'), ("Los Angeles",'2')]
def render_city( self, context, (city, value) ):
context.fillSlots( 'value', value )
context.fillSlots( 'city', city )
'sequence' is a built-in renderer that takes a list as data, and repeats the
nested 'item' pattern with each element of the list as data. It also accepts
patterns 'header', 'footer', and 'divider', but those arn't useful in this
case.
The data_cityList is changed to return a list of tuples, otherwise you will
have no control over the order of the items.
Also, this uses <n:attr>, which was just added to svn less than a day ago,
and atm only works with xmlfile. This allows putting slots in attributes.
Alternately, for something that doesn't use <n:attr> and isn't as verbose,
you could do:
<select n:render='sequence' n:data='cityList'>
<option n:render='city' n:pattern='item'>
the contents of this node don't matter
</option>
<select>
and change render_city to return a literal stan tag:
def render_city( self, context, (city, value) ):
from nevow.tags import *
return option( value=value )[city]
On Tue, Apr 13, 2004 at 12:43:09AM -0400, jonathan vanasco wrote:
>
> This works, but I think the approach might be wrong.
> (the output is what I want it to be, but I think I may have implemented
> things counter to the way nevow was designed)
>
> Can someone please offer their input?
>
>
>
> template.html
> -------------------
> <tr>
> <td valign="top"> pick a city</td>
> <td>
> <select name="city">
> <span nevow:data="citiesList" nevow:render="city"></span>
> </select>
> </td>
> </tr>
>
>
> testing.py
> -------------------
> def data_citiesList(self, context, data):
> return {"New York":'1', "Los Angeles":'2'}
> def render_city(self, context, data):
> """Example of using stan to render a page.
> """
> return rend.stan(
> [
> T.option(value=v)[k] for (k,v) in
> data.items()
> ]
> )
>
> Output.html
> ----------------
> <tr>
> <td valign="top"> pick a city</td>
> <td>
> <select name="city">
> <option value=1>New York</option>
> <option value=2>Los Angeles</option>
> </select>
> </td>
> </tr>
>
>
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
More information about the Twisted-web
mailing list