[mod_python] Building a web application using Cheetah and mod_python

Sebastian Celis mod_python at turias.net
Tue Oct 31 22:50:46 EST 2006


Thanks so much, Graham.  That works perfectly.  And thank you for the
first response to my original e-mail.  I agree with you and think I
will write my own handler instead of using publisher.  It should help
make my code a lot cleaner and remove a lot of duplication.  And
thanks for helping to clear things up about the module importer in
3.2.  I'm looking forward to 3.3.

Cheers,
Sebastian


On 10/31/06, Graham Dumpleton <grahamd at dscpl.com.au> wrote:
> Graham Dumpleton wrote ..
> > Sebastian Celis wrote ..
> > > 3) Cheetah and #include
> > >
> > > I have read a lot about why mod_python sets the current working
> > > directory to '/', but this seems to be causing some larger problems
> > > when cheetah comes into play.  (3) and (4) discuss two issues that I
> > > am having trouble with.
> > >
> > > Here is /var/www/app/templates/testPage.tmpl:
> > > ###
> > > #include 'header_include.tmpl'
> > > <head>
> > > <title>$title</title>
> > > </head>
> > > <body>
> > > <p>$message</p>
> > > </body>
> > > #include 'footer_include.tmpl'
> > > ###
> > >
> > > However, as this actually tries to load /header_include.tmpl and
> > > /footer_include.tmpl instead of the ones located in my templates
> > > directory, this will not work.  I came up with two workarounds,
> > > however I don't really love either:
> >
> > Use something like:
> >
> >   from mod_python import apache
> >   from Cheetah.Template import Template
> >
> >   ### START NEW CODE
> >
> >   class PathResolver:
> >       def __init__(self, base):
> >           self.__base = base
> >       def __call__(self, path=None):
> >           if path:
> >              return os.path.join(self.__base, path)
> >           return None
> >
> >   ### FINISH NEW CODE
> >
> >   def index(req):
> >       req.content_type = "text/html"
> >
> >       dict = {'title': 'My Title!', 'message': 'Hello world!'}
> >       templateModule = apache.import_module("templates/myTemplate")
> >       t = getattr(templateModule, "myTemplate")()
> >       searchList = t.searchList()
> >       searchList.insert(0, dict)
> >
> >       ### START NEW CODE
> >
> >       # Override Servlet.serverSidePath so that it returns path
> >       # relative to the directory where we say module is located.
> >
> >       t.serverSidePath = PathResolver(os.path.dirname(templateModule.__file__))
> >
> >       ### FINISH NEW CODE
> >
> >       return(t.respond())
> >
> > This replaces the underlying method that is used calculate the actual
> > path when you use #include. By doing it in the servlet, you don't have
> > to do anything trick in the actual .tmpl file.
>
> Or even simpler:
>
>   def index(req):
>       req.content_type = "text/html"
>
>       dict = {'title': 'My Title!', 'message': 'Hello world!'}
>       templateModule = apache.import_module("templates/myTemplate")
>       t = getattr(templateModule, "myTemplate")()
>       searchList = t.searchList()
>       searchList.insert(0, dict)
>
>       ### START NEW CODE
>
>       t.serverSidePath = lambda x: os.path.join(os.path.dirname(templateModule.__file__), x)
>
>       ### FINISH NEW CODE
>
>       return(t.respond())
>
> Graham
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list