Can't get Mako engine in CherryPy to work

荒凉一梦 提交于 2019-12-08 09:21:54

问题


I need to set up a server using CherryPy and the Mako Template Engine, though I can't get the latter to work.

I started to integrate the code from >>here<< into my working CherryPy setup. Though in the end, I only see "Hello, ${username}!" as text instead of the inserted variable. Other information or examples I found via search or Google didn't solve that as well.

Since the code is quite long, I use pastebin to show it.

server.py

app/application.py << I put another version of the index module there, but I also tried it with on in the integration example linked above.

The content/index.html is this simple file:

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

Is there anything I didn't setup right?


回答1:


What I suggested you to do in the comment was in fact just a change of template engine instance. The rest is the same. Having a CherryPy tool to handle templating routine is very handy and gives you big flexibility at configuration.

#!/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)

Along the script create directory tree view/app, create index.html there with:

<!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>

Notes about the tool:

  • The decorator uses convention-over-configuration for template file name. It is classname/methodname.html. On occasion you may want to redefine the template with name keyword decorator argument.
  • The decorator expose your method itself, no need to decorate with cherrypy.expose,
  • You can use the tool as any other CherryPy tool in the configuration e.g. to make all methods under /app/some/path to render data with corresponding templates.


来源:https://stackoverflow.com/questions/26618770/cant-get-mako-engine-in-cherrypy-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!