cgi, reusing html. common problem?
Steve Holden
steve at holdenweb.com
Thu Sep 1 21:57:56 EDT 2005
John M. Gabriele wrote:
> I'm putting together a small site using Python and cgi.
>
> (I'm pretty new to this, but I've worked a little with
> JSP/servlets/Java before.)
>
> Almost all pages on the site will share some common (and
> static) html, however, they'll also have dynamic aspects.
> I'm guessing that the common way to build sites like this
> is to have every page (which contains active content) be
> generated by a cgi script, but also have some text files
> hanging around containing incomplete html fragments which
> you read and paste-in as-needed (I'm thinking:
> header.html.txt, footer.html.txt, and so on).
>
> Is that how it's usually done? If not, what *is* the
> usual way of handling this?
>
There are a million ways to solve this particular problem, despite
Python's "TSBOAPOOOWTDI" (see "import this") philosophy (because the
philosophy is addressing primitive programming rather than application
frameworks).
You could do something as simple as writing a module "webPage" that
defines a function page(path, content) that does something along the
lines of:
def page(path, content):
return """\
<html>
<title>If you want titles, add another argument</title>
</head>
<body>
<this would be code to build a nav bar, omitting a hypertext link for
the path given as an argument - I'm just ignoring it in this example>
%s
</body>
</html>
""" % content
Then in your generation routines you can build up content in the
traditional way by generating individual fragments of HTML and appending
them to a list. So you start with
content = []
then for every fragment you generate you do
content.append(fragment)
and finally your content is generated with something like
content = webPage.page("siteroot/subdir/page1.html", "".join(content))
If you don't care that a page contains a navbar link to itself (a sign
of web immaturity, but by no means inexcusable) then you don't even need
to pass the page's path into the function.
Hope this gives you a few ideas. This problem has been considered by
many people, but clearly no outstanding solution has yet been devised,
otherwise we'd all be using it.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list