|
Graham Dumpleton
grahamd at dscpl.com.au
Tue Oct 31 17:51:53 EST 2006
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
|