项目作者: lixxu

项目描述 :
Jinja2 support for sanic
高级语言: Python
项目地址: git://github.com/lixxu/sanic-jinja2.git
创建时间: 2017-01-24T06:33:22Z
项目社区:https://github.com/lixxu/sanic-jinja2

开源协议:BSD 3-Clause "New" or "Revised" License

下载


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

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from sanic import Sanic
  4. from sanic_session import Session, InMemorySessionInterface
  5. from sanic_jinja2 import SanicJinja2
  6. app = Sanic()
  7. session = Session(app, interface=InMemorySessionInterface())
  8. jinja = SanicJinja2(app, session=session)
  9. #
  10. # Specify the package name, if templates/ dir is inside module
  11. # jinja = SanicJinja2(app, pkg_name='sanicapp')
  12. # or use customized templates path
  13. # jinja = SanicJinja2(app, pkg_name='sanicapp', pkg_path='other/templates')
  14. # or setup later
  15. # jinja = SanicJinja2()
  16. # jinja.init_app(app)
  17. @app.route("/")
  18. @jinja.template("index.html")
  19. async def index(request):
  20. jinja.flash(request, "success message", "success")
  21. jinja.flash(request, "info message", "info")
  22. jinja.flash(request, "warning message", "warning")
  23. jinja.flash(request, "error message", "error")
  24. jinja.session(request)["user"] = "session user"
  25. return dict(greetings="Hello, template decorator!")
  26. @app.route("/normal")
  27. async def normal_index(request):
  28. jinja.flash(request, "success message", "success")
  29. jinja.flash(request, "info message", "info")
  30. jinja.flash(request, "warning message", "warning")
  31. jinja.flash(request, "error message", "error")
  32. jinja.session(request)["user"] = "session user"
  33. return jinja.render(
  34. "normal_index.html", request, greetings="Hello, tempalte render!"
  35. )
  36. @app.route("/sync-handler")
  37. @jinja.template("index.html")
  38. def sync_hander(request):
  39. jinja.flash(request, "success message", "success")
  40. jinja.flash(request, "info message", "info")
  41. jinja.flash(request, "warning message", "warning")
  42. jinja.flash(request, "error message", "error")
  43. jinja.session(request)["user"] = "session user"
  44. return dict(greetings="Hello, sync handler!")
  45. if __name__ == "__main__":
  46. app.run(host="0.0.0.0", port=8000, debug=True, auto_reload=True)