betty.jinja2.filter module - Betty documentation

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

# betty.jinja2.filter module

Provide Betty’s default Jinja2 filters.

async betty.jinja2.filter.filter_file(context: Context, file: File) → str

  Preprocess a file for use in a page.

  Returns:

    The public path to the preprocessed file. This can be used on a web page.

async betty.jinja2.filter.filter_flatten(values_of_values: Iterable[Iterable[_T]]) → AsyncIterator[_T]

  Flatten an iterable of iterables into a single iterable.

betty.jinja2.filter.filter_format_datey(context: Context, datey: Datey) → str

  Format a date or a date range.

betty.jinja2.filter.filter_format_degrees(degrees: int) → str

  Format geographic coordinates.

betty.jinja2.filter.filter_hashid(source: str) → str

  Create a hash ID.

betty.jinja2.filter.filter_html_lang(context: Context, localized: LocalizedStr) → str | Markup

  Optionally add the necessary HTML to indicate the localized string has a different locale than the surrounding HTML.

async betty.jinja2.filter.filter_image_resize_cover(context: Context, filey: File | FileReference, size: tuple[int, None] | tuple[None, int] | tuple[int, int] | None = None, *, focus: tuple[int, int, int, int] | None = None) → str

  Preprocess an image file for use in a page.

  Returns:

    The public path to the preprocessed file. This can be embedded in a web page.

betty.jinja2.filter.filter_json(data: Any, indent: int | None = None) → str

  Convert a value to a JSON string.

betty.jinja2.filter.filter_localize(context: Context, localizable: Localizable) → str

  Localize a value using the context’s current localizer.

async betty.jinja2.filter.filter_localized_url(context: Context, resource: Any, locale: str | Locale | None = None, media_type: str | None = None, **kwargs: Any) → str

  Generate a localized URL for a localizable resource.

async betty.jinja2.filter.filter_map(context: Context, values: Iterable[Any], *args: Any, **kwargs: Any) → Any

  Map an iterable’s values.

  This mimics Jinja2’s built-in map filter, but allows macros as callbacks.

betty.jinja2.filter.filter_negotiate_has_dates(context: Context, has_dates: Iterable[HasDate], date: Datey | None) → HasDate | None

  Try to find an object whose date falls in the given date.

  Parameters:

    date – A date to select by. If `None`, then today’s date is used.

betty.jinja2.filter.filter_negotiate_localizeds(context: Context, localizeds: Iterable[Localized]) → Localized | None

  Try to find an object whose locale matches the context’s current locale.

betty.jinja2.filter.filter_paragraphs(eval_ctx: EvalContext, text: str) → str | Markup

  Convert newlines to <p> and <br> tags.

  Taken from http://jinja.pocoo.org/docs/2.10/api/#custom-filters.

betty.jinja2.filter.filter_public_css(context: Context, public_path: str) → None

  Add a CSS file to the current page.

betty.jinja2.filter.filter_public_js(context: Context, public_path: str) → None

  Add a JavaScript file to the current page.

betty.jinja2.filter.filter_select_has_dates(context: Context, has_dates: Iterable[HasDate], date: Datey | None) → Iterator[HasDate]

  Select all objects whose date falls in the given date.

  Parameters:

    date – A date to select by. If `None`, then today’s date is used.

betty.jinja2.filter.filter_select_localizeds(context: Context, localizeds: Iterable[Localized], include_unspecified: bool = False) → Iterable[Localized]

  Select all objects whose locale matches the context’s current locale.

  Parameters:

    include_unspecified – If True, the return value includes all objects that do not have a locale specified.

betty.jinja2.filter.filter_sort_localizeds(context: Context, localizeds: Iterable[Localized], localized_attribute: str, sort_attribute: str) → Iterable[Localized]

  Sort localized objects.

async betty.jinja2.filter.filter_static_url(context: Context, resource: Any, absolute: bool = False) → str

  Generate a static URL for a static resource.

async betty.jinja2.filter.filter_unique(values: Iterable[_T]) → AsyncIterator[_T]

  Iterate over an iterable of values and only yield those values that have not been yielded before.

async betty.jinja2.filter.filters() → Mapping[str, Callable[..., Any]]

  Define the available filters.

  This is internal. It MAY be used anywhere in Betty’s source code, but MUST NOT be used by third-party code.