[英]How can you split templates in mako in several files/directories?
我試圖了解如何拆分在多個目錄中使用 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
您聲明您的目錄結構是 /home/user/myapp/templates
但你告訴 Mako 查看 /templates
也許將代碼更改為:目錄=[ '/home/user/myapp/templates', '/home/user/myapp/templates/base', ],
我通常將模板拆分為每頁模板和全局模板示例:
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/
以獲得更通用的模板
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.