core.templates

Integrates the Chameleon template language.

This is basically a copy of more.chameleon, with the additional inclusion of a gettext translation function defined by onegov.core.i18n.

To use a chameleon template, applications have to specify the templates directory, in addition to inheriting from onegov.core.framework.Framework.

For example:

from onegov.core.framework import Framework

class App(Framework):
    pass

@App.template_directory()
def get_template_directory():
    return 'templates'

@App.path()
class Root:
    pass

@App.html(model=Root, template='index.pt')
def view_root(self, request):
    return {
        'title': 'The Title'
    }

The folder can either be a directory relative to the app class or an absolute path.

Attributes

AUTO_RELOAD

BOOLEAN_HTML_ATTRS

Classes

PageTemplate

PageTemplateFile

TemplateLoader

Extends the default page template loader with the ability to

MacrosLookup

Takes a list of search paths and provides a lookup for macros.

Functions

get_default_vars(request, content[, ...]) → dict[str, Any]

get_template_loader(template_directories, settings) → TemplateLoader

Returns the Chameleon template loader for templates with the extension

get_chameleon_render(loader, name, original_render) → collections.abc.Callable[[dict[str, Any], core.request.CoreRequest], T]

Returns the Chameleon template renderer for the required template.

render_template(template, request, content[, ...]) → markupsafe.Markup

Renders the given template. Use this if you need to get the rendered

render_macro(macro, request, content[, ...]) → markupsafe.Markup

Renders a chameleon.zpt.template.Macro like this:

Module Contents

core.templates.AUTO_RELOAD[source]
core.templates.BOOLEAN_HTML_ATTRS[source]
class core.templates.PageTemplate(*args: Any, **kwargs: Any)[source]

Bases: chameleon.PageTemplate

class core.templates.PageTemplateFile(*args: Any, **kwargs: Any)[source]

Bases: chameleon.PageTemplateFile

core.templates.get_default_vars(request: core.request.CoreRequest, content: collections.abc.Mapping[str, Any], suppress_global_variables: bool = False) → dict[str, Any][source]
class core.templates.TemplateLoader[source]

Bases: chameleon.PageTemplateLoader

Extends the default page template loader with the ability to lookup macros in various folders.

formats[source]
property macros: MacrosLookup[source]
property mail_macros: MacrosLookup[source]
class core.templates.MacrosLookup(search_paths: collections.abc.Iterable[_typeshed.StrPath], name: str = 'macros.pt')[source]

Takes a list of search paths and provides a lookup for macros.

This means that when a macro is access through this lookup, it will travel up the search path of the template loader, to look for the macro and return with the first match.

As a result, it is possible to have a macros.pt file in each search path and have them act as if they were one file, with macros further up the list of paths having precedence over the macros further down the path.

For example, given the search paths ‘foo’ and ‘bar’, foo/macros.pt could define ‘users’ and ‘page’, while bar/macros.pt could define ‘users’ and ‘site’. In the lookup this would result in ‘users’ and ‘page’ being loaded loaded from foo and ‘site’ being loaded from bar.

lookup[source]
__getitem__(name: str) → chameleon.zpt.template.Macro[source]
core.templates.get_template_loader(template_directories: list[str], settings: dict[str, Any]) → TemplateLoader[source]

Returns the Chameleon template loader for templates with the extension .pt.

core.templates.get_chameleon_render[T](loader: TemplateLoader, name: str, original_render: collections.abc.Callable[[str, core.request.CoreRequest], T]) → collections.abc.Callable[[dict[str, Any], core.request.CoreRequest], T][source]

Returns the Chameleon template renderer for the required template.

core.templates.render_template(template: str, request: core.request.CoreRequest, content: dict[str, Any], suppress_global_variables: bool | Literal['infer'] = 'infer') → markupsafe.Markup[source]

Renders the given template. Use this if you need to get the rendered value directly. If oyu render a view, this is not needed!

By default, mail templates (templates strting with ‘mail_’) skip the inclusion of global variables defined through the template_variables directive.

core.templates.render_macro(macro: chameleon.zpt.template.Macro, request: core.request.CoreRequest, content: dict[str, Any], suppress_global_variables: bool = True) → markupsafe.Markup[source]

Renders a chameleon.zpt.template.Macro like this:

layout.render_macro(layout.macros['my_macro'], **vars)

This code is basically a stripped down version of this: https://github.com/malthe/chameleon/blob/257c9192fea4b158215ecc4f84e1249d4b088753/src/chameleon/zpt/template.py#L206.

As such it doesn’t treat chameleon like a black box and it will probably fail one day in the future, if Chameleon is refactored. Our tests will detect that though.