HTML Generation

J Kenneth King james at agentultra.com
Fri Apr 3 16:33:52 CEST 2009


Tino Wildenhain <tino at living-examples.com> writes:

> Hi Mike,
>
>
> Mike wrote:
>> Hello all,
>>
>> I'm writing a web app and wanted to do some html generation (I
>> really do not like to maintain or write html).
>>
>> I'm thinking of writing a dsl based on the following:
>>
>> def html():
>>     return
>>
>> def a():
>>     return
>>
>> def body():
>>     return
>
> That would be writing HTML just another way and also
> mixing code and representation, this is generally not a
> good idea.
>
> If you really don't want to maintain HTML along with
> your code, I'd suggest an approach like
>
> Zope Page Templates
>
> http://en.wikipedia.org/wiki/Zope_Page_Templates#Zope_Page_Templates
>
> which can be used outside Zope as well, (even with many other languages)
>
> For example: http://zpt.sourceforge.net/
>
> Regards
> Tino

You could also look at CL-WHO for inspiration.

I feel the same way about templates -- the seperation is unnecessary
complexity. One knowledge domain for everything please. We're not
building the tower of Babel here.

I use Python through all of my desktop application development without
switching to markup languages and stylesheets and other scripting
languages (at least VERY rarely).

Why is the web any different?

It's rather somewhat trivial to implement a simple grammar in Python for
describing a page using Python data structures and Pythonic idioms.

I've toyed with it using a base "Tag" metaclass which creates the
__str__() representation for a Tag-metaclassed object based on the name
of the class.

It would look something like this:

code:
------------------------------------------------------------------------

from tags import html, head, meta, title, body, div, p, a

mypage = html(
             head(
                 meta(attrs={'http-equiv': "Content-Type",
                             'content': "text/html;"}),
                 title("My Page")),
             body(attrs={'background': "#ffffff;"},
                 div(attrs={'id': "article-content"},
                     p(a(attrs={'href': "http://python.org"},
                                "Python.org")))))

tabbed_file(mypage, open('myfile.html', 'w'))
------------------------------------------------------------------------

Unfortunately Python isn't as malleable when it comes to syntax as Lisp,
but I don't think it's a lost endeavour.

Having to know a template language, markup, stylesheets, CSS, plus all
of Python and good web application design is a huge headache and puts
too many cooks in the kitchen. I'd rather 95% of the work be in pure
Python and keep the team small.

Just 0.02 monetary units.



More information about the Python-list mailing list