简体   繁体   English

Python:每个应用程序的Mako模板查找

[英]Python: Mako template lookups per app

I'm using cherrypy with Mako as a template engine. 我正在将Cherryko与Mako用作模板引擎。

I want Mako to lookup different directories based on what app is being requested. 我希望Mako根据所请求的应用程序查找不同的目录。

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. / system包含一些系统范围的模板,例如404页等。

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. 在尝试了解cherrypy / mako的同时,我使用了Twiseless作为参考,但是我对如何做到这一点感到困惑

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: 在我的主文件server.py中,执行以下操作:

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. MakoTemplatePlugin是该插件的稍作修改的版本,其名称与上面链接的Twiseless中的名称相同。

What this code does is set the TemplateLookup to use the default template directories from my global config file. 此代码的作用是将TemplateLookup设置为使用全局配置文件中的默认模板目录。 ie

  • /template /模板
  • /template/system /模板/系统

Then, each time an app is loaded, I call a function (setTemplateDirs) to update the directories where Mako searches. 然后,每次加载应用程序时,我都会调用一个函数(setTemplateDirs)来更新Mako搜索的目录。

I thought this would work, but it doesn't. 我以为这可以用,但不能用。 Initially I made the error of creating a new instance of MakoTemplatePlugin for each app. 最初,我为每个应用创建一个新的MakoTemplatePlugin实例时出错。 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. 首次调用时,它在错误的文件夹中搜索时触发404错误。

I instead made sure to pass a reference to the MakeTemplatePlugin to all of my apps. 相反,我确保将对MakeTemplatePlugin的引用传递给所有我的应用程序。 I thought if I ran setTemplateDirs each time each app is called, this would solve the problem... but it doesn't. 我以为如果每次调用每个应用程序时都运行setTemplateDirs ,这将解决问题……但事实并非如此。

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: 显然,这仅在首次加载应用程序时有效。我尝试将update函数调用移到单独的方法update并尝试为每个页面调用它,例如:

@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 . 感谢stephan提供了指向mako工具示例的链接: 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. 如果有人想知道,其基础是您在全局配置中定义tools.mako.directories ,然后可以在单个应用程序配置文件中覆盖它。

eg 例如

server.conf server.conf

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

site.conf 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. 我做了一些额外的工作,将相对URI转换为绝对路径,但是上面已经解释了症结所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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