Mon, 23 Oct 2006
An example of xhtml web templates using python and meld3
In the past I have used spambaye's pymeldlite but recently have been trying out meld3 which has a slightly different approach, and hopefully better performance. Here is a quick chunk of example code showing it in action, the type of thing I wish could have disected when I was starting to figure out how it all fits together. I have converted a simple html example form to work as an xhtml template with a python cgi backend:- The original cgi-lib example form
- The xhtml template created from the original using HTML tidy and a bit of editing to add meld:id tags, etc.
- The form itself running as cgi
.. and of course the source code to the cgi
#!/usr/bin/python # # simple example of using meld3 and python cgi module to process a html form using an # xhtml template. # from meld3 import parse_xmlstring from meld3 import parse_htmlstring from StringIO import StringIO import sys, cgi, os colours = ["chartreuse", "azure", "puce", "cornflower", "olive draub", "gunmetal", "indigo2", "blanched almond", "flesh", "ochre", "opal", "amber"] debugenabled = False template = "template.xhtml" # log to a file so we can see whats going on without interfering with cgi output, if debugenabled turned on def debug(debugoutput): if debugenabled: f = open(DEBUGDIR+"/debug.out",'a') f.write(debugoutput) f.close() def process_form(root, form): # do a pile of stuff, to the encoded template, according to values from the form. # set the action to submit this cgi script root.findmeld('form1').attributes(action=os.environ.get('SCRIPT_NAME')) if form.has_key('name'): root.findmeld('name').content("name entered was "+form.getvalue('name')) root.findmeld('nameprompt').attributes(value=form.getvalue('name')) if form.has_key('quest'): root.findmeld('quest').content("name entered was "+form.getvalue('quest')) root.findmeld('questprompt').attributes(value=form.getvalue('quest')) if form.has_key('text'): root.findmeld('comment').content("name entered was "+form.getvalue('text')) root.findmeld('commentbox').content(form.getvalue('text')) if form.has_key('swallow'): root.findmeld('swallowtype').content("The swallow chosen was "+form.getvalue('swallow')) if form.getvalue('swallow') == 'african': root.findmeld('africanswallow').attributes(checked="") if form.getvalue('swallow') == 'continental': root.findmeld('continentalswallow').attributes(checked="") if form.has_key('color'): colourselected = form.getvalue('color') root.findmeld('colour').content(colourselected+" was the colour chosen") else: colourselected = '' iterator = root.findmeld('colouroption').repeat(colours) for element, item in iterator: element.attributes(value=item) element.content(item) if item == colourselected: element.attributes(selected="") return root try: xml = open(template).read() except: sys.stdout.write("Content-Type: text/html\n\n") sys.stdout.write("<html><b>Error reading temple file "+template+"</b></html>\n") sys.exit(0) try: root = parse_xmlstring(xml) except: t, v, tb = sys.exc_info() sys.stdout.write("Content-Type: text/html\n\n") sys.stdout.write("<html><b>XML Parse Error in template "+template+" :"+str(v)+" </b></html>") sys.exit(0) form = cgi.FieldStorage() root = process_form(root, form) sys.stdout.write("Content-Type: text/html\n\n") root.write_xhtml(sys.stdout) # print cgi.print_environ() # print cgi.print_form(form)
Posted at: 22:17 | category: /nerdy | # | 1 comments
