newbie : using python to generate web-pages
Logan
logan at phreaker.nospam
Fri Nov 28 15:19:56 EST 2003
On Fri, 28 Nov 2003 10:57:31 -0800, biner wrote:
> I am taking up on the work of a coleague who developed a big perl
> script to generate a bunch of html files. I am taking over his task
> and I looked at his code. It is quite messy because he used simple
> "print" command to print the html code into files so the script is a
> mix of html an perl. Quite ugly.
>
> I don't know much about perl but I know that there are html
> shortcuts in the CGI module that could allow me to make the code more
> readable. Something like "print em(toto)" that would print
> "<em>toto</em>"
I don't really think that 'print em(toto)' is more readable than
'print "<em>toto</em>"'. But that seems to be a matter of taste.
I would always prefer the second way of writing HTML because I
can see all the closing tags, indentation etc. Makes finding bugs
easier.
> Is there anything like that in Python. I did not find anything yet.
> I don't know much about python (even less than perl) but I hear good
> things about it all the time so I would like to use this project to
> get familiar with python.
If there are not many places in your HTML where you have to substitute
strings, then an approach like the following might be enough:
html = """
<html>
<head>
<title>%s</title>
</head>
<body>
%s
</body>
</html>
"""
print html % ("The Title", "Content Area")
(You could also use placeholders like xxTitlexx, xxContentxx in
the string 'html' and replace them to create the output.)
I think, the above solution is really 'readable' :-)
If you have to generate 'complicated' HTML files (depending on
various options, with dynamic menus etc.), I would recommend to
use an XML/XSLT approach, combined with a Python script (e.g.
4Suite (Ft) or libxml2/libxslt; the first is a Python package,
the second has Python bindings and both provide nice and easy to
use XSLT-processors).
And finally, if you have to generate your HTML files dynamically
on a web server, you should use a totally different approach; but
that would be another thread :-)
HTH, L.
--
mailto: logan at phreaker(NoSpam).net
More information about the Python-list
mailing list