Skip to content

Mako

This extension provides support for Mako templates.

Usage

To use mako templates, first install the mako extra:

pip install selva[mako]

Then activate the extension in the configuration file:

extensions:
  - selva.ext.templates.mako

To render templates, inject the selva.ext.templates.mako.MakoTemplate dependency and call its respond method:

from typing import Annotated
from selva.di import Inject
from selva.ext.templates.mako import MakoTemplate
from selva.web import get

@get
async def index(request, template: Annotated[MakoTemplate, Inject]):
    context = {"title": "Index"}
    await template.respond(request.response, "index.html", context)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Jinja Templates</title>
</head>
<body>
    <h1>${title}</h1>
</body>
</html>

Render templates to string

The MakoTemplate class provide methods to render templates into a str, instead of rendering to the response.

The method MakoTempate.render accepts a template name and returns a string with the rendered template.

The method MakoTempate.render_str accepts a template string, compiles it and returns the result.

rendered = template.render("template.html", {"variable": "value"})
rendered = template.render_str("${variable}", {"variable": "value"})

Configuration

Mako can be configured through the settings.yaml. For example, to activate filesystem checks:

templates:
  mako:
    directories:
      - resources/mako
    filesystem_checks: true

Full list of settings:

templates:
  mako:
    directories:
      - resources/templates
    module_directory: ""
    filesystem_checks: false
    collection_size: 100
    format_exceptions: false
    # dotted path to a python function
    error_handler: "package.module.function"
    encoding_errors: "strict" # or "ignore", "replace", "xmlcharrefreplace", "htmlentityreplace"
    cache_enabled: true
    cache_impl: "beaker"
    # dotted path to a python variable
    cache_args: "package.module:variable"
    # dotted path to a python function
    modulename_callable: "package.module.function"
    # dotted path to a python function
    module_writer: "package.module.function"
    default_filters: []
    buffer_filters: []
    strict_undefined: false
    imports: []
    future_imports: []
    enable_loop: true
    input_encoding: "utf-8"
    # dotted path to a python function
    preprocessor: "package.module.function"
    # dotted path to a python class
    lexer_cls: "package.module.Class"
    # dotted path to a python function
    include_error_handler: "package.module.function"