2017-04-26 04:10:46 +01:00
|
|
|
# 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-05-13 15:01:35 +01:00
|
|
|
from sanic.response import html
|
2017-04-26 04:10:46 +01:00
|
|
|
from jinja2 import Environment, PackageLoader, select_autoescape
|
2016-12-13 06:18:33 +00:00
|
|
|
|
|
|
|
app = Sanic(__name__)
|
|
|
|
|
2017-04-16 13:50:07 +01:00
|
|
|
# Load the template environment with async support
|
|
|
|
template_env = Environment(
|
2017-04-26 04:10:46 +01:00
|
|
|
loader=PackageLoader('jinja_example', 'templates'),
|
|
|
|
autoescape=select_autoescape(['html', 'xml']),
|
2017-04-16 13:50:07 +01:00
|
|
|
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):
|
2017-04-26 04:10:46 +01:00
|
|
|
rendered_template = await template.render_async(
|
|
|
|
knights='that say nih; asynchronously')
|
2017-05-13 15:01:35 +01:00
|
|
|
return html(rendered_template)
|
2016-12-13 06:18:33 +00:00
|
|
|
|
|
|
|
|
2017-05-13 15:01:35 +01:00
|
|
|
app.run(host="127.0.0.1", port=8080)
|