[Grok-dev] XML output using a page template?

Martijn Faassen faassen at startifact.com
Sun Feb 20 05:45:48 EST 2011


Hi there,

On 02/16/2011 08:40 AM, Sascha Boch wrote:
> Quick question - I'd like to use a page template to output XML data. How do I do this?

Just as a note: I tend not to use page templates for this at all, but 
instead use lxml's facilities: the E constructor is useful, along with 
things like SubElement.

That's not to say a template language cannot be used for this 
successfully, but I'd just like to point out an alternative.

Stuff like this:

from lxml.builder import E
from lxml import etree

class Something(grok.REST):
     grok.context(MyModel)

     def GET(self):
         foo = E.foo("blah")
         bqar = E.bar("something", some_attr="hoi")
         foo.append(bar)
         return etree.tostring(foo, encoding='UTF-8')

Regards,

Martijn



More information about the Grok-dev mailing list