quixote is an old-ish but still fascinating framework -- it basically "embeds HTML in Python" (rather than vice versa, as all popular templating systems do).
One simple example from the overview:
def format_row [html] (head, value):
"<tr valign=top align=left>\n"
" <th align=left>%s</th>\n" % head
" <td>%s</td>\n" % value
"</tr>\n"
In Python proper, the first of those strings would be the docstring, the others would be ignored, and the [html]
part would be a syntax error. In Quixote
, the [html]
marks this function as being "PTL" (Python Template Language) rather than Python proper, the file extension to use for modules with such functions is .ptl
, but they can still be import
ed from Python and those strings are output.
I doubt you want to adopt Quixote
in preference to modern Python templating approaches, but it does make for interesting reading, IMHO.
Further along similar lines is nevow (though it's more geared to generating XML, not HTML per se), esp. stan, where the canonical example is...:
>>> from nevow import flat, stan
>>> html = stan.Tag('html')
>>> p = stan.Tag('p')
>>> someStan = html[ p(style='font-family: Verdana;')[ "Hello, ", "world!" ] ]
>>> flat.flatten(someStan)
'<html><p style="font-family: Verdana;">Hello, world!</p></html>'
Kind of "even cooler"... because you don't have to worry about closing tags correctly;-).
In the end, though, for production work templating systems like jinja2 or mako are typically preferred these days -- the main practical reason being the better separation of presentation logic (in the template) from other layers (in Python code proper) than they offer wrt the "embed HTML/XML within Python" approaches, I guess.