[Twisted-web] Re: NevowSite and renderString (was: really beginner example)

Matt Goodall matt at pollenation.net
Thu Apr 8 04:05:39 MDT 2004

On Wed, 2004-04-07 at 21:28, Ellers wrote:


> I'm having trouble with lists now - in particular, I want to display a list and have each item be a link. I looked at the Nevow calendar example but I can't follow it.
> In my page class I have this (excerpt):
> 	def data_names(self, context, data):
> 		# this is a list of strings
> 		return self.names
> 	def getURLForName( self, name ):
> 		# a crappy demo of how I can parameterise getting the URL
> 		# for a given name
> 		return "/data/%s" % name
> 	def render_link(self, context, data):
> 		# would it be better to do something like:
> 		#
> 		#    return T.a(href=url)[title],
> 		#
> 		# ?
> 		#
> 		name = ??
> 		url = self.getURLForName( name )
> 		return "<a href='%s'>%s</a>" % ( url, title )
> I know this code isn't great, but the thing I'm stuck on right now is how do I get the name argument - from the data parameter?

The sequence renderer causes each item in the sequence to be passed to
the renderer as the data arg. So in your code "name = data" would work.
Although ... you don't appear to be calling the "link" renderer from the
HTML template you included, it's calling the standard string renderer

Creating the HTML fragment and returning it from render_link() is a bad
idea as the string will be xml-escaped, i.e. '<' will be replaced with
'&lt;', '>' will be replaced with '&gt;', etc. You should definitely use
the stan tags in this case, in fact the "return T.a ..." in the comment
is spot on.

NOTE: If you really, really want to send some HTML in a string then wrap
it in a tags.xml object which will pass the string straight through. For
instance,  i.e. return T.xml("<a href='%s'>%s</a>" % ( url, title )).

> Oh, and I probably shouldn't be calling it render_link... sorry guys, I get lost with Nevow a bit! :)
> The html fragment I'm using is:
> 		<p>This is called <span nevow:data="title" nevow:render="string" /></p>
> 		<p>This is a list of stuff:<br/>
> 		<ol nevow:data="names" nevow:render="sequence">
> 			<span nevow:pattern="header">HEADER</span>
> 			<li nevow:pattern="item"><span nevow:render="string" /></li>
> 			<span nevow:pattern="divider">-----</span>
> 			<div nevow:pattern="empty">Nothing.</div>
> 			<span nevow:pattern="footer">FOOTER</span>
> 		</ol>
> 		</p>
> The title span is working really well - and many thanks here go to Alex Levy!

I'll let you have a crack at getting things working now but you may also
want to look at the nevow.url module. It makes constructing URLs
*really* easy.

Hope this helps.

Cheers, Matt

    /  \__     Matt Goodall, Pollenation Internet Ltd
    \__/  \    w: http://www.pollenation.net
  __/  \__/    e: matt at pollenation.net
 /  \__/  \    t: +44 (0)113 2252500
 \__/  \__/
 /  \          Any views expressed are my own and do not necessarily
 \__/          reflect the views of my employer.

More information about the Twisted-web mailing list