API References

jinja2_sanic.get_env(app, *, app_key='sanic_jinja2_environment')[source]

Get Jinja2 env by app_key.

Parameters:
  • app – a Sanic instance
  • app_key – a optional key for application instance. If not provided, default value will be used.
jinja2_sanic.render_string(template_name, request, context, *, app_key='sanic_jinja2_environment')[source]

Render a string by filling Template template_name with context. Returns a string.

Parameters:
  • template_name – template name.
  • request – a parameter from web-handler, sanic.request.Request instance.
  • context – context for rendering.
  • app_key – a optional key for application instance. If not provided, default value will be used.
jinja2_sanic.render_template(template_name, request, context, *, app_key='sanic_jinja2_environment', encoding='utf-8', headers=None, status=200)[source]

Return sanic.response.Response which contains template template_name filled with context. Returned response has Content-Type header set to ‘text/html’.

Parameters:
  • template_name – template name.
  • request – a parameter from web-handler, sanic.request.Request instance.
  • context – context for rendering.
  • encoding – response encoding, ‘utf-8’ by default.
  • status – HTTP status code for returned response, 200 (OK) by default.
  • app_key – a optional key for application instance. If not provided, default value will be used.
jinja2_sanic.setup(app, *args, app_key='sanic_jinja2_environment', context_processors=(), filters=None, **kwargs)[source]

Initialize jinja2.Environment object.

Parameters:
  • app – a Sanic instance
  • app_key – an optional key for application instance. If not provided, default value will be used.
  • context_processors – context processors that will be used in request middlewares.
  • and kwargs (args) – will be passed to environment constructor.
jinja2_sanic.template(template_name, *, app_key='sanic_jinja2_environment', encoding='utf-8', headers=None, status=200)[source]

Decorate web-handler to convert returned dict context into sanic.response.Response filled with template_name template.

Parameters:
  • template_name – template name.
  • request – a parameter from web-handler, sanic.request.Request instance.
  • context – context for rendering.
  • encoding – response encoding, ‘utf-8’ by default.
  • status – HTTP status code for returned response, 200 (OK) by default.
  • app_key – a optional key for application instance. If not provided, default value will be used.