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
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.
Kent
Michele Simionato
-- http://mail.python.org/mailman/listinfo/python-list