[Twisted-web] Alternative to <nevow:attr>

Donovan Preston dp at divmod.org
Tue Oct 19 14:19:51 MDT 2004

On Oct 19, 2004, at 2:51 AM, Andrew Bennetts wrote:
> TAL does allow this, in two ways (that I know of):
> First, there's the hackish way:
>     <a tal:attributes="href python:'/users/' + context/username + 
> '/images'"
>      />
> The second way is to put this sort of complex code a view:
>     class ViewBlah:
>         def __init__(self, context, request):
>             self.context = context
>             self.request = request
>         def imagesPath(self):
>             return '/users/' + self.context.username + '/images'
> then you can do:
>     <a tal:attributes="href view/imagesPath" />

Which is exactly what Nevow allows you to do very easily, as well. 
Currently, if you want to put this type of information in python code, 
you can use a technique similar to your second example:

def render_imagesPath(self, ctx, username):
	return ctx.tag(href=['/users/', username, '/images'])

If you'd rather put that information in the template, you can do it 
using nevow:attr:

<a><nevow:attr name="href">/users/<nevow:slot name="username" 

Again, the reason I arrived at this syntax is because it doesn't 
require you to learn any new substitution syntax and the full range of 
nevow functionality is available. It's consistent.

I don't see what the huge advantage of dreid's proposed 
nevow:attributes syntax is, in fact I'm not even sure how it would 
work. I'm against it mostly because it's another mechanism users have 
to learn, and is only useful for one small use case.

Another possibility is a python renderer:

<a nevow:render="python ctx.tag(href=["/users/", slot('username'), 
"/images"])" />

The implementation would be pretty simple:

def render_python(self, ctx, code):
	def evalIt(ctx, data):
		return eval(code, {'ctx': ctx, 'data': data})
	return evalIt


More information about the Twisted-web mailing list