from aiohttp import web from mako.lookup import TemplateLookup from mako.template import Template from mako.exceptions import TopLevelLookupException templates = TemplateLookup(directories=['./templates'], module_directory='./__templates_cache__') def render_template(name, status=200, **data): try: text = templates.get_template(f'{name}.html').render_unicode(**data) except TopLevelLookupException: text = '404: template not found.' status = 404 response = web.Response(text=text, status=status, charset = 'utf-8', content_type = 'text/html') return response