Skip to content

lixxu/sanic-jinja2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sanic-jinja2

Jinja2 support for sanic

Example

Installation

python3 -m pip install sanic-jinja2

Features

sanic-jinja2 supports:

  • Flask-like flash method
  • i18n and Babel support
  • @jinja.template syntax
  • session extension support
  • factory pattern init_app method for creating apps

Usage

NOTICE:

If you want to use flash and get_flashed_messages, you need setup session first.

Currently, app and request are hooked into jinja templates, thus you can use them in template directly.

And, from version 0.3.0 enable_async is default to True. If you need sync functions, use jinja.render_sync, jinja.render_string_sync

Python3.5 does not support new async syntax, so 0.5.0 disable async back, sorry.

BUG: request should not be set to global environment, so you need use request['flash'] instead of jinja.flash and need pass request to render to use get_flashed_messages.

Examples

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sanic import Sanic
from sanic_session import Session, InMemorySessionInterface
from sanic_jinja2 import SanicJinja2

app = Sanic()

session = Session(app, interface=InMemorySessionInterface())

jinja = SanicJinja2(app, session=session)
#
# Specify the package name, if templates/ dir is inside module
# jinja = SanicJinja2(app, pkg_name='sanicapp')
# or use customized templates path
# jinja = SanicJinja2(app, pkg_name='sanicapp', pkg_path='other/templates')
# or setup later
# jinja = SanicJinja2()
# jinja.init_app(app)

@app.route("/")
@jinja.template("index.html")
async def index(request):
    jinja.flash(request, "success message", "success")
    jinja.flash(request, "info message", "info")
    jinja.flash(request, "warning message", "warning")
    jinja.flash(request, "error message", "error")
    jinja.session(request)["user"] = "session user"
    return dict(greetings="Hello, template decorator!")


@app.route("/normal")
async def normal_index(request):
    jinja.flash(request, "success message", "success")
    jinja.flash(request, "info message", "info")
    jinja.flash(request, "warning message", "warning")
    jinja.flash(request, "error message", "error")
    jinja.session(request)["user"] = "session user"
    return jinja.render(
        "normal_index.html", request, greetings="Hello, tempalte render!"
    )


@app.route("/sync-handler")
@jinja.template("index.html")
def sync_hander(request):
    jinja.flash(request, "success message", "success")
    jinja.flash(request, "info message", "info")
    jinja.flash(request, "warning message", "warning")
    jinja.flash(request, "error message", "error")
    jinja.session(request)["user"] = "session user"
    return dict(greetings="Hello, sync handler!")


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, debug=True, auto_reload=True)