【问题标题】:Can't get Mako engine in CherryPy to work无法让 CherryPy 中的 Mako 引擎工作
【发布时间】:2014-12-24 11:22:15
【问题描述】:

我需要使用 CherryPy 和 Mako 模板引擎设置服务器,但我无法让后者工作。

我开始将>>here<< 的代码集成到我工作的 CherryPy 设置中。虽然最后,我只看到“你好,${username}!”作为文本而不是插入的变量。我通过搜索或 Google 找到的其他信息或示例也没有解决这个问题。

由于代码比较长,我用pastebin来展示。

server.py

app/application.py

content/index.html 就是这个简单的文件:

<html>
<body>
Hello, ${username}!
</body>
</html>

有什么我没有设置正确的吗?

【问题讨论】:

  • “进入我的工作 CherryPy 设置” -- 你提到的工作 CherryPy 设置是什么?
  • 好吧,我可以让我的 html 文件和 python 脚本来处理它。我使用 CherryPy 设置作为本地服务器。我上传并分享了我认为相关的文件。
  • question 中,您可以找到适用于 Jinja2 和 Cheetah 的 CherryPy 工具。如果您了解 Mako 的基础知识,您应该能够相应地更改模板引擎对象及其调用。
  • 所以我终于意识到“目录”似乎无法正常工作。我只是把我的测试文件放在我项目的根文件夹中,它现在可以工作了。有没有办法在这里插入绝对路径? “os.path.dirname(os.path.abspath(file)”这会从我的 python 安装中返回 lib 文件夹。

标签: python templates cherrypy mako


【解决方案1】:

我在评论中建议你做的实际上只是模板引擎实例的更改。其余的都是一样的。拥有一个 CherryPy 工具来处理模板例程非常方便,并且为您提供了很大的配置灵活性。

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import types

import cherrypy
import mako.lookup


path   = os.path.abspath(os.path.dirname(__file__))
config = {
  'global' : {
    'server.socket_host' : '127.0.0.1',
    'server.socket_port' : 8080,
    'server.thread_pool' : 8
  }
}


class TemplateTool(cherrypy.Tool):

  _engine = None
  '''Mako lookup instance'''


  def __init__(self):
    viewPath     = os.path.join(path, 'view')
    self._engine = mako.lookup.TemplateLookup(directories = [viewPath])

    cherrypy.Tool.__init__(self, 'before_handler', self.render)

  def __call__(self, *args, **kwargs):
    if args and isinstance(args[0], (types.FunctionType, types.MethodType)):
      # @template
      args[0].exposed = True
      return cherrypy.Tool.__call__(self, **kwargs)(args[0])
    else:
      # @template()
      def wrap(f):
        f.exposed = True
        return cherrypy.Tool.__call__(self, *args, **kwargs)(f)
      return wrap

  def render(self, name = None):
    cherrypy.request.config['template'] = name

    handler = cherrypy.serving.request.handler
    def wrap(*args, **kwargs):
      return self._render(handler, *args, **kwargs)
    cherrypy.serving.request.handler = wrap

  def _render(self, handler, *args, **kwargs):
    template = cherrypy.request.config['template']
    if not template:
      parts = []
      if hasattr(handler.callable, '__self__'):
        parts.append(handler.callable.__self__.__class__.__name__.lower())
      if hasattr(handler.callable, '__name__'):
        parts.append(handler.callable.__name__.lower())
      template = '/'.join(parts)

    data     = handler(*args, **kwargs) or {}
    renderer = self._engine.get_template('{0}.html'.format(template))

    return renderer.render(**data)


cherrypy.tools.template = TemplateTool()


class App:

  @cherrypy.tools.template
  def index(self):
    return {'foo': 'bar'}

  @cherrypy.tools.template(name = 'app/index')
  def manual(self):
    return {'foo': 'baz'}

  @cherrypy.tools.json_out()
  @cherrypy.expose
  def offtopic(self):
    '''So it is a general way to apply a format to your data'''
    return {'foo': 'quz'}


if __name__ == '__main__':
  cherrypy.quickstart(App(), '/', config)

沿着脚本创建目录树view/app,在那里创建index.html

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv='content-type' content='text/html; charset=utf-8' />
    <title>Test</title>
  </head>
  <body>
    <p>Foo is: <em>${foo}</em></p>
  </body>
</html>

关于工具的说明:

  • 装饰器对模板文件名使用约定优于配置。它是classname/methodname.html。有时您可能希望使用 name 关键字装饰器参数重新定义模板。
  • 装饰器暴露你的方法本身,不需要用cherrypy.expose装饰,
  • 您可以将该工具用作配置中的任何其他 CherryPy 工具,例如让/app/some/path下的所有方法用对应的模板渲染数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多