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.
pip install sanja
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(
...)
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"}
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):
...
...
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}}
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).
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}}