betty.jinja2 package¶
Submodules¶
- betty.jinja2.filter module
filter_base64()filter_file()filter_flatten()filter_format_datey()filter_format_degrees()filter_hashid()filter_image()filter_json()filter_localize()filter_map()filter_negotiate_dateds()filter_negotiate_localizeds()filter_paragraphs()filter_public_css()filter_public_js()filter_select_dateds()filter_select_localizeds()filter_sort_localizeds()filter_static_url()filter_unique()filter_url()filter_walk()
- betty.jinja2.test module
Module contents¶
Provide rendering utilities using Jinja2.
- class betty.jinja2.EntityContexts[source]¶
Bases:
objectTrack the current entity contexts.
To allow templates to respond to their environment, this class allows our templates to set and get one entity per entity type for the current context.
Use cases include rendering an entity label as plain text if the template is in that entity’s context, but as a hyperlink if the template is not in the entity’s context.
- Parameters:
entities (
betty.model.Entity)
- __init__(*entities: betty.model.Entity) None[source]¶
- Parameters:
entities (
betty.model.Entity)
- class betty.jinja2.Environment[source]¶
Bases:
EnvironmentBetty’s Jinja2 environment.
- Parameters:
app (
betty.app.App)
- __init__(app: betty.app.App)[source]¶
- Parameters:
app (
betty.app.App)
- property context_class: type[Context]¶
The template context holds the variables of a template. It stores the values passed to the template and also the names the template exports. Creating instances is neither supported nor useful as it’s created automatically at various stages of the template evaluation and should not be created by hand.
The context is immutable. Modifications on
parentmust not happen and modifications onvarsare allowed from generated template code only. Template filters and global functions marked aspass_context()get the active context passed as first argument and are allowed to access the context read-only.The template context supports read only dict operations (get, keys, values, items, iterkeys, itervalues, iteritems, __getitem__, __contains__). Additionally there is a
resolve()method that doesn’t fail with a KeyError but returns anUndefinedobject for missing variables.
- filters: dict[str, Callable[..., Any]]¶
- async from_file(file_path: pathlib._local.Path) jinja2.environment.Template[source]¶
Create a
jinja2.Templateout of the given Jinja2 file path.This method is intended for rendering individual files once. It MUST NOT be used for reusable templates.
- Parameters:
file_path (
pathlib._local.Path)- Return type:
- globals: dict[str, Any]¶
- tests: dict[str, Callable[..., bool]]¶
- class betty.jinja2.Jinja2Provider[source]¶
Bases:
objectIntegrate an
betty.app.extension.Extensionwith the Jinja2 API.- property filters: dict[str, Callable[[...], Any]]¶
Jinja2 filters provided by this extension.
Keys are filter names, and values are the filters themselves.
- property globals: dict[str, Any]¶
Jinja2 globals provided by this extension.
Keys are the globals’ names, and values are the globals’ values.
- class betty.jinja2.Jinja2Renderer[source]¶
Bases:
RendererRender content as Jinja2 templates.
- Parameters:
environment (
betty.jinja2.Environment)configuration (
betty.project.ProjectConfiguration)
- __init__(environment: betty.jinja2.Environment, configuration: betty.project.ProjectConfiguration)[source]¶
- Parameters:
environment (
betty.jinja2.Environment)configuration (
betty.project.ProjectConfiguration)
- async render_file(file_path: pathlib._local.Path, *, job_context: betty.job.Context | None = None, localizer: betty.locale.Localizer | None = None) pathlib._local.Path[source]¶
Render a single file.
- Return type:
pathlib._local.Path- Returns:
The file’s new path, which may have been changed, e.g. a renderer-specific extension may have been stripped from the end.
- Parameters:
file_path (
pathlib._local.Path)job_context (
betty.job.Context|None)localizer (
betty.locale.Localizer|None)
- betty.jinja2.context_app(context: jinja2.runtime.Context) betty.app.App[source]¶
Get the current app from the Jinja2 context.
- Parameters:
context (
jinja2.runtime.Context)- Return type:
- betty.jinja2.context_job_context(context: jinja2.runtime.Context) betty.job.Context | None[source]¶
Get the current job context from the Jinja2 context.
- Parameters:
context (
jinja2.runtime.Context)- Return type:
- betty.jinja2.context_localizer(context: jinja2.runtime.Context) betty.locale.Localizer[source]¶
Get the current localizer from the Jinja2 context.
- Parameters:
context (
jinja2.runtime.Context)- Return type: