templates.py 683 B

1234567891011121314151617181920
  1. from aiohttp import web
  2. from mako.lookup import TemplateLookup
  3. from mako.template import Template
  4. from mako.exceptions import TopLevelLookupException
  5. templates = TemplateLookup(directories=['./templates'], module_directory='./__templates_cache__')
  6. def render_template(name, status=200, **data):
  7. try:
  8. text = templates.get_template(f'{name}.html').render_unicode(**data)
  9. except TopLevelLookupException:
  10. text = '404: template not found.'
  11. status = 404
  12. response = web.Response(text=text,
  13. status=status,
  14. charset = 'utf-8',
  15. content_type = 'text/html')
  16. return response