Jinja2 template renderer for Sanic
Project description
sanic-aioja
aioja template renderer for Sanic.
This library has been inspired by so many other projects (sanic-jinja2, django-jinja). So thanks all for the inspiration.
Install
pip install sanic-aioja
Features
- Debug mode
- Babel support
@jinja2.template
decorator- Shortcut methods:
globals
,filters
,tests
,extensions
andpolicies
- Built-in
url
andstatic
global functions - Ability to precompile templates
Example
from sanic import Sanic
from sanic.response import html
from sanic_aioja import Jinja2, FileSystemLoader
app = Sanic("sanic_aioja")
jinja2 = Jinja2(
app,
# use DebugUndefined
debug=True,
# precompile templates on server start.
# See jinja2.Environment.compile_templates()
precompile=True,
precompile_path=".jinja2.zip",
# Jinja2 options
trim_blocks=True,
lstrip_blocks=True,
loader=FileSystemLoader("./templates"),
)
# Lets extend environment with some globals
jinja2.globals({
"token": "extensions.token",
}).policies({
"ext.i18n.trimmed": True,
})
@app.route('/')
@jinja2.template("index.html")
async def index(request):
return {
"header": "Sanic-aioja",
"array": ["Red", "Green", "Blue"],
}
@app.route('/render/')
async def index(request):
content = await jinja2.render_to_string(request, "index.html", {
"header": "Sanic-aioja",
"array": ["Red", "Green", "Blue"],
})
return html(content)
if __name__ == "__main__":
app.run()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sanic-aioja-0.0.1.tar.gz
(6.8 kB
view hashes)
Built Distribution
Close
Hashes for sanic_aioja-0.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efcac998a0d81140a1d76a04ccb57bf5315eb6f328580bcda413aaad79e1bf66 |
|
MD5 | bf09f3a54baf984b7ef67506f6bf9a61 |
|
BLAKE2b-256 | 523eb83367a9aca9d845d9f5d47cd0461829edfec622ab4bf82477b21d63c883 |