Skip to main content

This module aims to make bringing Jinja templates to Sanic to be easy.

Project description

sanja

This module aims to make bringing Jinja templates to Sanic to be easy.

It is entirelly up to You
how You will configure your Jinja template environment.
Great freedom :-)

It consist of only two dozens lines of simple code.

Installation.

pip install sanja  

First You have to configure Your Sanic app so it holds Jinja template environment instance.

app = sanic.Sanic("Some app")  

sanja_conf_app(  
    app,  
    # There go normal parameters like for jijna2.Environment(),  
    # for example:  
    auto_reload=True,  
    loader=jinja2PrefixLoader({  
        'some_package': jinja_PackageLoader("some_package")}))  

By default this Jinja template environment is held in:

app.config['JINJA_ENV']  

so equaly well You could do:

app.config['JINJA_ENV'] = jinja2.Environment(  
    auto_reload=True,  
    loader=jinja2PrefixLoader({  
        'some_package': jinja_PackageLoader("some_package")}))  

But if You wish to change it,
or have more then one Jinja template environment,
then just:

sanja_conf_app(  
    app,  
    jinja_template_env_name="JINJA_ENV_2",  
    ...)  

or:

app.config['JINJA_ENV_2'] = jinja2.Environment(  
    ...)  

Then You can just use Jinja rendering and Sanic response.

To do so simply decorate your request handler,
for example:

@app.route("/some/url")  
@sanja.render("some_package/some_template.html.jinja", "html")  
async def some_view(request):  
    ...  
    return {'jijna': "context"}  
  • some_view function has to return jinja "context" instance.
  • In first sanja.render() parameter provide jinja template.
    (In this example it has to be under
    some_package/templates/some_template.html.jinja
    because it is how we configured here jinja template environment,
    so having any isues please refer to Jinja documentation).
  • Second sanja.render() parameter coresponds to sanic.response "kind",
    so if doubts plese refer to Sanic documentation.
    Possible values are:
    • "text"
    • "html"
    • "json"
    • "raw"

And if You want to use other Jinja template environment,
that You have configured before,
you can do so:

@app.route("/some/url2")  
@sanja.render("some_package/some_template.html.jinja", "html", jinja_template_env_name="JINJA_ENV_2")  
async def some_view(request):  
    ...  
    return {'jijna': "context"}  

Yo can also use it for Class-Based Views.

In decorators class variable:

class YourView(sanic.views.HTTPMethodView):  
    decorators = [sanja.render(...)]  

    ...  

or per http method:

class YourView(sanic.views.HTTPMethodView):  

    @sanja.render(...)  
    async def get(self, request):  
        ...  

    ...  

NOTE for json format.

In order to return json format,
your template should be either hand crafted proper json, e.g:

{  
    "some": "thing"  
}  

or e.g whole template can be just one variable turned into json like:

{{some_variable|tojson}}  

SENDING ADDITIONAL CONTEXT TO TEMPLATES.

Sending template context to template.

You may find usefull to have access to template context from within template.

To enable it for all templates (generated by particular jinja env),
just provide:

update_templates_with_its_context=True  

to conf_app() function.

To enable it for particular generated template,
just provide:

update_template_with_its_context=True  

to @render() decorator.

Then in template You can access all template context with:

{{get_template_context()}}  

or ask for value for just some specific key:

{{get_template_contex("key")}}  

(None is returned if key is not in template context).

Sending any extra context to template.

To send e.g.: x=5 to all templates (generated by particular jinja env),
just provide:

update_jinja_env_globals_with={'x': 5}  

to conf_app() function.

To send e.g.: x=5 to particular generated template,
just provide:

update_template_globals_with={'x': 5}  

to @render() decorator.

Then in template You can access it with:

{{x}}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanja-1.0.5.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sanja-1.0.5-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file sanja-1.0.5.tar.gz.

File metadata

  • Download URL: sanja-1.0.5.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.0.post20200704 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for sanja-1.0.5.tar.gz
Algorithm Hash digest
SHA256 41cd5a38edaa43a74345fd95ffbe8242a5795a17fd680b0e09bba52e808347b4
MD5 4d3d2e8301ea5cd111ae65ae844ac239
BLAKE2b-256 0111467ef2e694e44db8e56981dd773ebd1ea1e2b06c3dfb6846020d5de254f5

See more details on using hashes here.

File details

Details for the file sanja-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: sanja-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.0.post20200704 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for sanja-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 5259120f1481b42be23d7414048e987124712bea4196ad01fb6668898fa9675c
MD5 9492f99bab3038444877b540e844cb6d
BLAKE2b-256 f88ba258134fb9b69f1628abd8a1f67b508ac5d99457bb5cf4a55156302141d5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page