Page layouts in mod_python?

Graham Dumpleton grahamd at dscpl.com.au
Tue Dec 19 18:34:15 EST 2006


Bruno Desthuilliers wrote:
> Michael a écrit :
> > Hey everyone,
> >
> > Is it possible to automatically insert headers/footers using
> > mod_python?
> > I will be not be using PSP's, so I cannot use the PSP/include solution.
> > Furthermore, the header will be dynamic; it won't be a static HTML
> > page.
> >
> > In short, I've been looking for a page layout facility using
> > mod_python.
> > (Similar to the layout capability in Ruby on Rails.)
>
> mod_python is mainly a librairy exposing the apache API to Python - not
> a full-stack db-to-web framework. What you want here is a HTML
> templating system. There are quite a lot available in Python - look for
> SimpleTAL, Genshi, Myghty, Jinja, Cheetah, etc...

Contrary to the above advise, you could actually use mod_python to do
what you want and you do not need a full stack to do it. Whether you
would want to use just mod_python is another question and the answer
would depend on a bit on what form the HTML files you want to modify
are in or what is generating them.

The first option available is to write an Apache output filter using
mod_python which takes the content of the page and searches for the
<body> and </body> tags and inserts at those points the appropriate
header/footer. Using this scheme, because you are looking for tags
already within the HTML page, you do not need to modify the original
HTML source files. Because it is an output filter, the technique will
work on both static files and on files which have been generated
dynamically by PHP, mod_perl, mod_python or any other Apache response
handler capable of generating dynamic pages.

For this first technique, you do not even have to use mod_python if you
didn't want to, as there are other Apache modules which have been
designed to specifically use this technique to insert headers and
footers in pages. These other modules are written in C and so are
probably going to be quicker as well as more flexible without you
having to do too much hard work.

The second option, although not strictly mod_python specific, is to
enable server side include processing for HTML files. For this to work
you would need to modify the original HTML source files or what is
generating them to add in appropriate SSI directives to include the
header/footer. Again, because SSI is performed as an output filter,
source files can be static or dynamically generated.

If using basic SSI, the include of the header/footer to get dynamic
information would be done as a virtual include. That is, it generates
an Apache subrequest to a handler which generates the header/footer.
This handler could be implemented as a CGI script, mod_python handler,
mod_perl handler or even PHP.

Alternatively, in mod_python 3.3 (soon to be released), one can use the
new ability to use Python code in conjunction with SSI. For dynamic
header/footer snippets, this would involve adding callouts to pages to
Python code setup using mod_python.

For an example of header/footer generation using Python code, see:


http://www.dscpl.com.au/wiki/ModPython/Articles/BasicsOfServerSideIncludes

If you particularly want to use mod_python, for further discussion on
this topic and what may be best for what you want to do, I would
suggest you might take the conversation over to the mod_python user
mailing list. Details for the list are on the mod_python web site.

Graham




More information about the Python-list mailing list