Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lixxu/sanic-jinja2
Jinja2 support for sanic
https://github.com/lixxu/sanic-jinja2
jinja2 sanic
Last synced: 3 months ago
JSON representation
Jinja2 support for sanic
- Host: GitHub
- URL: https://github.com/lixxu/sanic-jinja2
- Owner: lixxu
- License: bsd-3-clause
- Created: 2017-01-24T06:33:22.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-11-11T07:14:24.000Z (almost 2 years ago)
- Last Synced: 2024-07-06T13:23:37.418Z (4 months ago)
- Topics: jinja2, sanic
- Language: Python
- Size: 82 KB
- Stars: 115
- Watchers: 7
- Forks: 17
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.TXT
Awesome Lists containing this project
- awesome-sanic - Sanic-Jinja2
README
# sanic-jinja2
Jinja2 support for sanic
![Example](example/example.png)
## Installation
`python3 -m pip install sanic-jinja2`
## Features
`sanic-jinja2` supports:
- Flask-like `flash` method
- i18n and Babel support
- `@jinja.template` syntax
- [session extension](https://github.com/xen/sanic_session) 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_syncPython3.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
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-from sanic import Sanic
from sanic_session import Session, InMemorySessionInterface
from sanic_jinja2 import SanicJinja2app = 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)```