ANN: htmlbuilder.py - another HTML generator

G

Gerard Flanagan

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Windows (vers 1st September 2004), see www.w3.org">
<title>Test Page</title>
</head>
<body>
<!-- Begin Header -->
<div id="header">
<h1>The Website At The End of The Universe</h1>
</div>
<!-- Begin Content -->
<div id="content">
<h3>Welcome Arthur Dent!!</h3>
</div>
<!-- Begin Footer -->
<div id="footer">Cheetah says the date is 10/3/06</div>
</body>
</html>

Gerard
 
G

Gerard Flanagan

Gerard said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top