a jinja2 template renderer for Sanic
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs
jinja2_sanic
tests
.gitignore
.travis.yml
LICENSE
README.rst
conda.yml
readthedocs.yml
setup.py
ubuild.py
uranium

README.rst

jinja2-sanic

build status coverage status

a jinja2 template renderer for Sanic. It supports:

  • function based web handlers
  • class-based views
  • decoractors for convenient useage

You can find out more here:

http://jinja2-sanic.readthedocs.io/en/latest/

This lib has been strongly influenced by aiohttp-jinja2.

Install

pip3 install jinja2-sanic

Quick Start

from sanic import Sanic
from sanic.views import HTTPMethodView
from sanic.exceptions import ServerError

app = Sanic("sanic_jinja2_render")

# Setup jinja2 environment
template = "<html><body><h1>{{Player}}</h1>{{Category}}</body></html>"
jinja2_sanic.setup(
    app,
    loader=jinja2.DictLoader(
        {
            "templates.jinja2": template
        }
    )
)

# Usage in function based web handlers
@app.route("/")
@jinja2_sanic.template("templates.jinja2")
async def func(request):
    return {
        "Player": "CR7",
        "Category": "Soccer",
    }

# Usage in class-based views
class SimpleView(HTTPMethodView):

    @jinja2_sanic.template("templates.jinja2")
    async def get(self, request):
        return {
            "Player": "CR7",
            "Category": "Soccer",
        }

# register class based view routes
app.add_route(SimpleView.as_view(), "/")

# Start Server
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

Development

jinja2-sanic accepts contributions on GitHub, in the form of issues or pull requests.

Build.

./uranium

Run unit tests.

./uranium test