【问题标题】:How can you split templates in mako in several files/directories?如何将 mako 中的模板拆分为多个文件/目录?
【发布时间】:2019-06-14 00:59:47
【问题描述】:

我正在尝试了解如何拆分在多个目录中使用 Mako 和 CherryPy 的项目。我准备了以下目录结构:

[FOLDER] /home/user/myapp
         |- main.py
         |- app.config
         |- server.config
[FOLDER] /home/user/myapp/templates
[FOLDER] /home/user/myapp/templates/base
         |- index.html
         |- sidebar_menu.html
[FOLDER] /home/user/myapp/config
         |- templates.py

/home/user/myapp/templates 中会有不同的模板组织在目录中。

/home/user/myapp/config 下,我有以下文件:templates.py,代码如下:

# -*- coding: utf-8 -*-

import mako.template
import mako.lookup

# Templates
templates_lookup = mako.lookup.TemplateLookup(
    directories=[
        '/templates',
        '/templates/base',
    ],
    module_directory='/tmp/mako_modules',
    input_encoding='utf-8', 
    output_encoding='utf-8', 
    encoding_errors='replace'
)

def serve_template(templatename, **kwargs):
    mytemplate = templates_lookup.get_template(templatename)
    print(mytemplate.render(**kwargs))

/home/user/myapp 下会有以下main.py 文件:

# -*- coding: utf-8 -*-

import os
import cherrypy
import mako.template
import mako.lookup
import config.templates

# Main Page
class Index(object):
    @cherrypy.expose
    def index(self): 
        t = config.templates.serve_template('index.html')
        print(t)
        return t

cherrypy.config.update("server.config")
cherrypy.tree.mount(Index(), '/', "app.config")
cherrypy.engine.start()

当我启动应用程序并访问 / 时,我收到以下消息:

500 Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request.

Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\mako\lookup.py", line 247, in get_template
    return self._check(uri, self._collection[uri])
KeyError: 'index.html'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\cherrypy\_cprequest.py", line 628, in respond
    self._do_respond(path_info)
  File "C:\Python37\lib\site-packages\cherrypy\_cprequest.py", line 687, in _do_respond
    response.body = self.handler()
  File "C:\Python37\lib\site-packages\cherrypy\lib\encoding.py", line 219, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "C:\Python37\lib\site-packages\cherrypy\_cpdispatch.py", line 54, in __call__
    return self.callable(*self.args, **self.kwargs)
  File ".....\myapp\main.py", line 18, in index
    t = config.templates.serve_template('index.html')
  File ".....\myapp\config\templates.py", line 19, in serve_template
    mytemplate = templates_lookup.get_template(templatename)
  File "C:\Python37\lib\site-packages\mako\lookup.py", line 261, in get_template
    "Cant locate template for uri %r" % uri)
mako.exceptions.TopLevelLookupException: Cant locate template for uri 'index.html'

Powered by CherryPy 18.1.0 

因此,尽管我们提供了目录,但基本上 Mako 似乎无法找到 index.html。我想我不明白 Mako 在查找中的用途。

注意:程序实际上是在Windows中运行的,我上面使用了UNIX文件结构只是为了让文件结构更容易阅读。

Python 3.7.2
CherryPy 18.1.0
Mako 1.0.7

【问题讨论】:

    标签: python cherrypy mako


    【解决方案1】:

    您声明您的目录结构是 /home/user/myapp/templates

    但你是在告诉 Mako 往里看 /模板

    也许将代码更改为: 目录= [ '/home/user/myapp/模板', '/home/user/myapp/templates/base', ],

    【讨论】:

      【解决方案2】:

      我通常将模板拆分为每页模板和全局模板 示例:

      src/
      ├── constants.py
      ├── home
      │   └── user
      │       └── myapp
      │           ├── app.config
      │           ├── main.mako
      │           ├── main.py
      │           └── server.config
      └── templates
          ├── e404.mako
          ├── e500.mako
          ├── footer.mako
          └── header.mako
      

      在这种情况下,我将始终使用查找目录导入全局文件

      # src/constants.py
      from mako.lookup import TemplateLookup
      mylookup = TemplateLookup(directories=['.', 'dir/to/src/templates/'])
      
      # home/user/myapp/main.py
      from src.constants import mylookup
      
      def main():
         if i_have_errer:
             template = mylookup.get_template('e500.mako')
         else:
             template = mylookup.get_template('main.mako')
      
         return template.render_unicode()
      

      '.' 将首先在当前目录中查找 templates/ 将在全局 src/templates/ 中查找更通用的模板

      【讨论】:

        猜你喜欢
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        • 2011-12-09
        • 2017-01-02
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多