Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# betty.jinja2 package

## Submodules

- betty.jinja2.filter module
  - `filter_file()`
  - `filter_flatten()`
  - `filter_format_datey()`
  - `filter_format_degrees()`
  - `filter_hashid()`
  - `filter_image_resize_cover()`
  - `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()`
- betty.jinja2.test module
  - `test_date_range()`
  - `test_end_of_life_event()`
  - `test_entity()`
  - `test_has_file_references()`
  - `test_has_generated_entity_id()`
  - `test_has_links()`
  - `test_linked_data_dumpable()`
  - `test_start_of_life_event()`
  - `test_subject_role()`
  - `test_user_facing_entity()`
  - `test_witness_role()`

## Module contents

Provide rendering utilities using Jinja2.

class betty.jinja2.EntityContexts

  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.

  __init__(*entities: Entity) → None

class betty.jinja2.Environment

  Bases: `Environment`

  Betty’s Jinja2 environment.

  __init__(project: Project)
  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 on `vars` are allowed from generated template code only. Template filters and global functions marked as `pass_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 an `Undefined` object for missing variables.

  filters: dict[str, Callable[..., Any]]
  globals: dict[str, Any]
  property project: Project

    The current project.

  tests: dict[str, Callable[..., bool | Awaitable[bool]]]

class betty.jinja2.Jinja2Provider

  Bases: `object`

  Integrate an `betty.project.extension.Extension` with 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.

  new_context_vars() → dict[str, Any]

    Create new variables for a new `jinja2.runtime.Context`.

    Keys are the variable names, and values are variable values.

  property tests: dict[str, Callable[[...], bool]]

    Jinja2 tests provided by this extension.

    Keys are test names, and values are the tests themselves.

final class betty.jinja2.Jinja2Renderer

  Bases: `Renderer`

  Render content as Jinja2 templates.

  __init__(environment: Environment, configuration: ProjectConfiguration)
  property file_extensions: set[str]

    The extensions of the files this renderer can render.

  async render_file(file_path: Path, *, job_context: JobContext | None = None, localizer: Localizer | None = None) → Path

    Render a single file.

    Returns:

      The file’s new path, which may have been changed, e.g. a renderer-specific extension may have been stripped from the end.

betty.jinja2.context_job_context(context: Context) → Context | None

  Get the current job context from the Jinja2 context.

betty.jinja2.context_localizer(context: Context) → Localizer

  Get the current localizer from the Jinja2 context.

betty.jinja2.context_project(context: Context) → Project

  Get the current project from the Jinja2 context.