sanic/examples/jinja_example/jinja_example.py

29 lines
765 B
Python
Raw Normal View History

# Render templates in a Flask like way from a "template" directory in
# the project
2016-12-13 06:18:33 +00:00
from sanic import Sanic
2017-04-11 21:34:55 +01:00
from sanic import response
from jinja2 import Environment, PackageLoader, select_autoescape
2016-12-13 06:18:33 +00:00
app = Sanic(__name__)
# Load the template environment with async support
template_env = Environment(
loader=PackageLoader('jinja_example', 'templates'),
autoescape=select_autoescape(['html', 'xml']),
enable_async=True
)
# Load the template from file
template = template_env.get_template("example_template.html")
2016-12-13 06:18:33 +00:00
@app.route('/')
async def test(request):
rendered_template = await template.render_async(
knights='that say nih; asynchronously')
return response.html(rendered_template)
2016-12-13 06:18:33 +00:00
app.run(host="0.0.0.0", port=8080, debug=True)