简体   繁体   中英

Python: Mako template lookups per app

I'm using cherrypy with Mako as a template engine.

I want Mako to lookup different directories based on what app is being requested.

Ie I have three 'apps': Site, Admin and Install.

They all have their own template folder, structure looking something like:

  • /template
  • /template/site
  • /template/admin
  • /template/install
  • /template/system

/system contains some system wide templates, like 404 pages, etc.

I'm using Twiseless as a reference whilst trying to get to grips with cherrypy / mako, but I'm stuck with how to do this.

Read on for a brief overview of how I've tried to do this, but a warning: I think I'm going about this completely the wrong way! :) So, if you have any ideas/pointers, it might be a good idea to save yourself the trouble of reading any further than this.

In my main file, server.py, I do something like:

from libs.plugins.template import MakoTemplatePlugin

engine = cherrypy.engine
makoTemplate = MakoTemplatePlugin(engine, self.base_dir)
setTemplateDirs(makoTemplate, self.template_path)

MakoTemplatePlugin is a slightly modified version of the plugin by the same name found in Twiseless, linked above.

What this code does is set the TemplateLookup to use the default template directories from my global config file. ie

  • /template
  • /template/system

Then, each time an app is loaded, I call a function (setTemplateDirs) to update the directories where Mako searches.

I thought this would work, but it doesn't. Initially I made the error of creating a new instance of MakoTemplatePlugin for each app. This just resulted in them all being called on each page load, starting with the first one instantiated, containing just the basic, non-app specific directories.

As this was called first, it was triggering a 404 error, as it was searching in the wrong folders.

I instead made sure to pass a reference to the MakeTemplatePlugin to all of my apps. I thought if I ran setTemplateDirs each time each app is called, this would solve the problem... but it doesn't.

I don't know where to put the function so it will run every time a page is requested...

eg

# /apps/site/app.py

import somemodule.setTemplateDirs

class Site(object, params):
    def __init__(self):
        self.params = params
        self.makoTemplate = params['makoTemplate']
        self.base_path = params['base_path']
        setTemplateDirs(self.makoTemplate, self.base_path, '', '/')

    @cherrypy.expose
    @cherrypy.tools.render(template='index.html')
    def index(self):
        pass

This obviously just works when the application is first loaded... I tried moving the update function call into a seperate method update and tried calling that for each page, eg:

@cherrypy.exposed
@cherrypy.tools.render(template='index.html')
@update
def index(self):
    pass

But this just gives me config related errors.

Rather than to continue to mess about with this, there must be an easier way.

How would you do it?

Thanks a lot,

Tom

I got this working. Thanks to stephan for providing the link to the mako tool example: http://tools.cherrypy.org/wiki/Mako .

I just modified that slightly to get it working.

If anyone's wondering, the basis of it is that you define tools.mako.directories in your global config, you can then override that in individual app config files.

eg

server.conf

...
tools.mako.directories: ['', 'system']
...

site.conf

...
tools.mako.directories: ['site', 'system']
...

I did some extra work to translate the relative URIs to absolute paths, but the crux of it is explained above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM