[mod_python] relative paths in templates

Graham Dumpleton grahamd at dscpl.com.au
Sun Feb 13 04:21:31 EST 2005


On 13/02/2005, at 7:31 PM, Jef Dodson wrote:

> So, my question is this: how do I deal with the general problem of 
> relative paths without
> resorting to changing all the links in my templates or putting a 
> <base> tag in all the templates?
> Keep in mind that these are static paths, so changing them 
> programmatically is not an option in
> this case.  There must be some really clean and elegant way to solve 
> this problem using the
> almighty httpd.conf file, right?!  Thanks!

Doesn't directly help you, but still may be of interest anyway, but 
what I do in
Vampire is provide a configuration file mechanism which as a side 
effect returns
some predefined default values including base urls which can be used in 
filling
out templates.

The idea is that one would put a configuration file in the top 
directory of that
part of your document tree, which notionally is the root of your web 
application.
Ideally this would also be where PythonHandler was defined. By default, 
generally
call this configuration file ".vampire".

In your content handler file anywhere below that root point, ie., 
including in
subdirectories, you then would say something like:

   def handler(req):
     config = vampire.loadConfig(req,".vampire")
     defaults = config.defaults()

The "loadConfig()" method will search back up the physical directory 
hierarchy
looking for the named configuration file. In doing that search, it is 
able to
derive various information which is filled out as defaults usable when 
extracting
items from the configuration file.

Specifically, the "defaults" variable is a dictionary containing:

       __handler_root__ --> Directory where PythonHandler directive was 
defined.
       __config_root__ --> Directory containing the config file found.
       __config_file__ --> Location of actual config file found.
       __config_mtime__ --> Modification time of the config file found.
       __baseurl_abs__ --> Base URL as absolute path.
       __baseurl_rel__  --> Base URL relative to the current request.

In terms of what you are asking, the important ones here are the base 
url values.
They are set to the URL which would be used to address the top 
directory of your
application. In one it is an absolute path from the root of your whole 
web server.
In the other it is a relative path in respect of the directory that 
that specific
request applied to.

In a templating scheme, one could populate the data available within 
the template
with these values and thus, depending on the syntax of your template 
scheme, you
could say the equivalent of:

   '<img src="%(__baseurl_rel__)s/images/my_image.gif">' % defaults

In other words, you write up all your templates with paths with respect 
to the top
level directory or your application. You use your templating syntax for 
value
substitution to automatically fill out the base url value base on that 
obtained
from the configuration search.

There is also another part of what you are doing which can be aided by 
the use
of what can actually be placed in the configuration file itself. Rather 
than having
to hard code a path into all your content handlers as to the path to 
your template
files, you could have a configuration file which contains:

   [Templates]

   library = %(__config_root__)s/templates

In your content handler you then say:

   def handler(req):
     config = vampire.loadConfig(req,".vampire")
     filepath = config.get("Templates","library")
     ...

By having the location defined in the configuration file in respect of 
the root of
your application, you can then easily move the directory where ever you 
want and
just change the location reference in one place and have that change 
reflected
automatically through all you content handlers.

There is obviously some cost in having such things be so dynamic, but 
frankly I
prefer making my applications easy to manage. I don't expect to be 
slashdotted
and if it was high load site, one would properly support it with 
adequate machines
and load balancing etc. :-)

Graham



More information about the Mod_python mailing list