recommended way of generating HTML from Python

Matt Goodall matt at pollenation.net
Mon Feb 21 09:52:10 EST 2005


On Mon, 2005-02-21 at 07:36 -0500, Kent Johnson wrote:
> Michele Simionato wrote:
> > The problem is a problem of standardization, indeed. There plenty of
> > recipes to
> > do the same job, I just would like to use a blessed one (I am teaching
> > a Python
> > course and I do not know what to recommend to my students).
> 
> Why not teach your students to use a template system?
> 
> > FWIW, here is a my version of the recipe (stripped down to the bare
> > essentials)
> > 
> > .def makeattr(dict_or_list_of_pairs):
> > .    dic = dict(dict_or_list_of_pairs)
> > .    return " ".join("%s=%r" % (k, dic[k]) for k in dic)
> > 
> > .class HTMLTag(object):
> > .    def __getattr__(self, name):
> > .        def tag(value, **attr):
> > .            """value can be a string or a sequence of strings."""
> > .            if hasattr(value, "__iter__"): # is iterable
> > .                value = " ".join(value)
> > .            return "<%s %s>%s</%s>\n" % (name, makeattr(attr), value,
> > name)
> > .        return tag
> > 
> > # example:
> > .html = HTMLTag()
> > 
> > .tableheader = ["field1", "field2"]
> > .tablebody = [["a1", "a2"],
> > .         ["b1", "b2"]]
> > 
> > .html_header = [html.tr(html.th(el) for el in tableheader)]
> > .html_table = [html.tr(html.td(el) for el in row) for row in tablebody]
> > .print html.table(html_header + html_table)
> 
> *Shudder*
> 
> I've written web pages this way (using a pretty nice Java HTML generation package) and I don't 
> recommend it. In my experience, this approach has several drawbacks:
> - as soon as the web page gets at all complex, the conceptual shift from HTML to code and back is 
> difficult.
> - It is hard to work with a designer. The designer will give you sample web pages which then have to 
> be hand-translated to code. Changes to the web page have to be located in the code.
> - There is no separation of content and presentation

Slightly off topic but, just to be clear, Nevow supports XHTML templates
*and* stan tag expressions.  Both are useful. As a general rule, I stick
to XHTML templates but I use stan for prototyping pages and when marking
up the XHTML templates gets so complicated that they might as well be
written in Python anyway.

Also, just because the HTML code is not in a .html file does not
necessarily mean that content and presentation are mixed up. For
instance, with stan (and probably the alternatives mentioned in this
thread) it's very easy to build a tag library that the "real" Python
code simply calls on to render page content.

> 
> IMO templating systems are a much better solution. They let you express HTML in HTML directly; you 
> communicate with a designer in a language the designer understands; you can separate content and 
> presentation.

Agreed. Although I would go further and say that it's important to
choose a templating system that allows the Python developer to annotate
XHTML templates using **valid XML**, i.e. no "for x in y" loops, no "if
foo" conditionals, no "i = 0" variable setting, no expression
evaluations, etc.


<advocacy>
The lovely thing about Nevow is that it encourages good separation -
HTML is HTML and logic is Python, as it should be - but does not get in
the way when breaking the rules is necessary or just a lot easier.
</advocacy>


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 Python-list mailing list