From b3101d339e00dfacb4b077d417a99c246c6473d1 Mon Sep 17 00:00:00 2001 From: Matt Maybeno Date: Tue, 25 Apr 2017 20:10:46 -0700 Subject: [PATCH] Fix typo for jinja example and converted to dir --- examples/{ => jinja_example}/jinja_example.py | 15 ++++++++------- examples/jinja_example/requirements.txt | 8 ++++++++ .../jinja_example/templates/example_template.html | 10 ++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) rename examples/{ => jinja_example}/jinja_example.py (55%) create mode 100644 examples/jinja_example/requirements.txt create mode 100644 examples/jinja_example/templates/example_template.html diff --git a/examples/jinja_example.py b/examples/jinja_example/jinja_example.py similarity index 55% rename from examples/jinja_example.py rename to examples/jinja_example/jinja_example.py index cf517030..430940c5 100644 --- a/examples/jinja_example.py +++ b/examples/jinja_example/jinja_example.py @@ -1,15 +1,16 @@ -# Render templates in a Flask like way from a "template" directory in the project +# Render templates in a Flask like way from a "template" directory in +# the project from sanic import Sanic from sanic import response -from jinja2 import Evironment, PackageLoader, select_autoescape +from jinja2 import Environment, PackageLoader, select_autoescape app = Sanic(__name__) # Load the template environment with async support template_env = Environment( - loader=jinja2.PackageLoader('yourapplication', 'templates'), - autoescape=jinja2.select_autoescape(['html', 'xml']), + loader=PackageLoader('jinja_example', 'templates'), + autoescape=select_autoescape(['html', 'xml']), enable_async=True ) @@ -19,9 +20,9 @@ template = template_env.get_template("example_template.html") @app.route('/') async def test(request): - data = request.json - rendered_template = await template.render_async(**data) + rendered_template = await template.render_async( + knights='that say nih; asynchronously') return response.html(rendered_template) -app.run(host="0.0.0.0", port=8080, debug=True) \ No newline at end of file +app.run(host="0.0.0.0", port=8080, debug=True) diff --git a/examples/jinja_example/requirements.txt b/examples/jinja_example/requirements.txt new file mode 100644 index 00000000..c7cd5408 --- /dev/null +++ b/examples/jinja_example/requirements.txt @@ -0,0 +1,8 @@ +aiofiles==0.3.1 +httptools==0.0.9 +Jinja2==2.9.6 +MarkupSafe==1.0 +sanic==0.5.2 +ujson==1.35 +uvloop==0.8.0 +websockets==3.3 diff --git a/examples/jinja_example/templates/example_template.html b/examples/jinja_example/templates/example_template.html new file mode 100644 index 00000000..b914ad64 --- /dev/null +++ b/examples/jinja_example/templates/example_template.html @@ -0,0 +1,10 @@ + + + + My Webpage + + +

Hello World

+

knights - {{ knights }}

+ +