Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dldevinc/sanic-aioja
aioja template renderer for Sanic
https://github.com/dldevinc/sanic-aioja
async jinja2 sanic
Last synced: 25 days ago
JSON representation
aioja template renderer for Sanic
- Host: GitHub
- URL: https://github.com/dldevinc/sanic-aioja
- Owner: dldevinc
- License: bsd-3-clause
- Created: 2020-10-16T13:58:06.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-16T14:32:41.000Z (about 4 years ago)
- Last Synced: 2024-11-18T23:56:20.427Z (about 2 months ago)
- Topics: async, jinja2, sanic
- Language: Python
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# sanic-aioja
[aioja](https://github.com/dldevinc/aioja) template renderer for Sanic.This library has been inspired by so many other projects such as
[sanic-jinja2](https://github.com/lixxu/sanic-jinja2) and
[django-jinja](https://github.com/niwinz/django-jinja).[![PyPI](https://img.shields.io/pypi/v/sanic-aioja.svg)](https://pypi.org/project/sanic-aioja/)
[![Build Status](https://travis-ci.org/dldevinc/sanic-aioja.svg?branch=master)](https://travis-ci.org/dldevinc/sanic-aioja)## Install
```
pip install sanic-aioja
```## Features
* Debug mode
* Babel support
* `@jinja2.template` decorator
* Shortcut methods: `globals`, `filters`, `tests`, `extensions` and `policies`
* Built-in `url` and `static` global functions
* Ability to precompile templates## Example
```python
from sanic import Sanic
from sanic.response import html
from sanic_aioja import Jinja2, FileSystemLoaderapp = 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"),
)# Let's 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()
```