The problem is that I wanted to use a templating system. One of cherrypy's advantages is that it is template-neutral. But it does have it's own templating system called cherrytemplate (how original). I basically want to display data in tabular form. Using cherrytemplate, this is a simple for loop:
<py-for="item in self.items"> <tr><td><py-eval="str(item.id)></td><td><py-eval="item.value"></td></tr> </py-for>
This works ok. It's a little annoying having to type all the "py-" stuff everywhere and it doesn't automatically coerse values to strings, so one must str() them. But other than that it's fairly straight-forward. But in addation I want to be able to manipulate each row via JavaScript, so I give each row an unique identifier. So the above example becomes:
<py-for="item in self.items"> <tr id="<py-eval='str(item.id)'>"><td><py-eval="str(item.id)></td><td><py-eval="item.value"></td></tr> </py-for>
Ok, this just looks weird (having a tag inside an attribute of another tag). In addition, it doesn't work. CherryTemplate (rightfully?) chokes on this bit of code. So what's the right way of doing this? I couldn't find a way of doing it with cherrytemplate. So then I switched templates to Cheetah. Cheetah is a lot easier on the eyes (and the fingers). The equivalent would look like this in Cheetah:
#for $item in $self.items <tr id="$item.id"><td>$item.id</td><td>$item.value</td></tr> #end for
Much nicer IMO. Unfortunately, this doesn't work either. It seems that Cheetah code isn't run "inline" the way cherrytemplate is, and self collides with another self inside Cheetah. Cheetah's solution for this is to provide a "search list" to the templating engine when it looks up an identifier. It's a hack but it works. So the way you get around it inside your class is this:
this = self return Template(s, searchList = [locals()]
At least that's the way I did it. And in your template, s, subsitute this for self:
#for $item in $this.items <tr id="$item.id"><td>$item.id</td><td>$item.value</td></tr> #end for
In summary, cherrypy's object publishing rocks, but templating sucks. I wish there was a combination of Cheetah's syntax and cherrytemplate's "inline" execution. The quixote templating system seems more natural. Quixote's templates are python with HTML embedded in it. CherryTemplate and Cheetah are HTML with python embedded. I prefer the former.
Of course there is also TAL, which, while really nice, seemed like overkill for me and I didn't want to go through all the TAL initialization just to write a for loop, even though TAL is more HTML-friendly (think of it as the opposite of quixote templates).I will continue to work with cherrypy. The jury is still out whether I will love it or hate it.