betty.jinja2 package¶
Submodules¶
- betty.jinja2.filter module
filter_file()
filter_flatten()
filter_format_datey()
filter_format_degrees()
filter_html_lang()
filter_image_resize_cover()
filter_json()
filter_localize()
filter_localized_url()
filter_map()
filter_negotiate_has_dates()
filter_negotiate_localizeds()
filter_paragraphs()
filter_public_css()
filter_public_js()
filter_select_has_dates()
filter_select_localizeds()
filter_sort_localizeds()
filter_static_url()
filter_unique()
filter_url()
filters()
- betty.jinja2.globals module
- betty.jinja2.test module
Module contents¶
Provide rendering utilities using Jinja2.
- class betty.jinja2.EntityContexts[source]¶
Bases:
object
Track 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.
- class betty.jinja2.Environment[source]¶
Bases:
ProjectDependentFactory
,Environment
Betty’s Jinja2 environment.
- __init__(project: Project, extensions: Sequence[Extension], assets: AssetRepository, entity_contexts: EntityContexts, filters: Mapping[str, Callable[..., Any]], tests: Mapping[str, Callable[..., bool]])[source]¶
This is private. It MUST NOT be used anywhere outside its containing scope.
- 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
parent
must not happen and modifications onvars
are 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 anUndefined
object for missing variables.
- async from_file(file_path: Path) Template [source]¶
Create a
jinja2.Template
out of the given Jinja2 file path.This method is intended for rendering individual files once. It MUST NOT be used for reusable templates.
- class betty.jinja2.Jinja2Provider[source]¶
Bases:
object
Integrate an
betty.project.extension.Extension
with the Jinja2 API.- property filters: Mapping[str, Callable[[...], Any]]¶
Jinja2 filters provided by this extension.
Keys are filter names, and values are the filters themselves.
- property globals: Mapping[str, Any]¶
Jinja2 globals provided by this extension.
Keys are the globals’ names, and values are the globals’ values.
- final class betty.jinja2.Jinja2Renderer[source]¶
Bases:
Renderer
,ProjectDependentFactory
,Plugin
Render content as Jinja2 templates.
- __init__(environment: Environment, configuration: ProjectConfiguration)[source]¶
- async classmethod new_for_project(project: Project) Self [source]¶
Create a new instance using the given project.
- classmethod plugin_id() MachineName [source]¶
Get the plugin ID.
IDs are unique per plugin type:
A plugin repository MUST at most have a single plugin for any ID.
Different plugin repositories MAY each have a plugin with the same ID.
- classmethod plugin_label() Localizable [source]¶
Get the human-readable short plugin label.
- betty.jinja2.context_job_context(context: Context) Context | None [source]¶
Get the current job context from the Jinja2 context.