ANN: htmlbuilder.py - another HTML generator

Gerard Flanagan grflanagan at yahoo.co.uk
Fri Mar 31 00:32:44 CEST 2006


Gerard Flanagan wrote:

> In the hope that it may be useful, a simple Html Generator:
>
>     http://gflanagan.net/site/python/htmlbuilder/htmlbuilder.py
>
> It requires 'elementtree' :
> http://www.effbot.org/zone/element-index.htm
>
> Example:
>
>     html = HtmlBuilder( doctype='strict')
>     page = html.page('Test Page')
>     page.comment('Begin Header')
>     page.template('HEADER')
>     page.comment('Begin Content')
>     page.template('CONTENT')
>     page.comment('Begin Footer')
>     page.template('FOOTER')
>
>     header = html.include( '../test/header.html' )
>     content = html.div(id='content')
>     content.h3('Welcome ').template('USER').literal('!!')
>     footer = html.div(id='footer')
>     footer.cheetah('Cheetah says the date is $DATE')
>
>     content %= [{ 'USER': 'Arthur Dent' }]
>     footer %= [{ 'DATE': '10/3/06' }]
>     page %= [{'HEADER': header,'CONTENT': content,'FOOTER': footer }]
>     print
>     print page
>

Another example using web.py (http://webpy.org):

(Only tested with web.py's standalone server)

import web
import sys
from myutils.htmlbuilder import HtmlBuilder, HtmlWriter, HtmlPage

urls = (
    '/(.*)', 'hello'
)

class hello(HtmlPage):

    def __init__(self):
        HtmlPage.__init__(self, 'Welcome Page')
        self.div.h3('Hello ').template('USER').literal('!!')

    def GET(self, name=None):
        if not name:
            name = 'World'
        self.fill( [ {'USER': name} ] )
        writer = HtmlWriter( sys.stdout )
        self.write( writer )
        

if __name__ == "__main__":
    web.run(urls)

Gerard




More information about the Python-list mailing list