Mako
This extension provides support for Mako templates.
Usage
To use mako templates, first install the mako
extra:
Then activate the extension in the configuration file:
To render templates, inject the selva.ext.templates.mako.MakoTemplate
dependency
and call its respond
method:
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:
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"