Module

rendering.template_functions.taxonomies

Taxonomy helper functions for templates.

Provides 4 functions for working with tags, categories, and related content.

Functions

register
Register taxonomy helper functions with Jinja2 environment.
2 None
def register(env: Environment, site: Site) -> None

Register taxonomy helper functions with Jinja2 environment.

Parameters 2

Name Type Default Description
env Environment
site Site
related_posts
Find related posts based on shared tags. PERFORMANCE NOTE: This function now uses pre-computed rel…
3 list[Any]
def related_posts(page: Any, all_pages: list[Any] | None = None, limit: int = 5) -> list[Any]

Find related posts based on shared tags.

PERFORMANCE NOTE: This function now uses pre-computed related posts for O(1) access. The old O(n²) algorithm is kept as a fallback for backward compatibility with custom templates.

RECOMMENDED: Usepage.related_postsdirectly in templates instead of calling this function.

Parameters 3

Name Type Default Description
page Any

Current page

all_pages list[Any] | None None

All site pages (optional, only needed for fallback)

limit int 5

Maximum number of related posts

Returns

list[Any]

List of related pages sorted by relevance

Example (NEW - recommended): {% set related = page.related_posts[:3] %}

Example (OLD - backward compatible): {% set related = related_posts(page, limit=3) %} {% for post in related %} <a href="{{ url_for(post) }}">{{ post.title }}</a> {% endfor %}

popular_tags
Get most popular tags sorted by count.
2 list[tuple[str, int]]
def popular_tags(tags_dict: dict[str, list[Any]], limit: int = 10) -> list[tuple[str, int]]

Get most popular tags sorted by count.

Parameters 2

Name Type Default Description
tags_dict dict[str, list[Any]]

Dictionary of tag -> pages

limit int 10

Maximum number of tags

Returns

list[tuple[str, int]]

List of (tag, count) tuples

tag_url
Generate URL for a tag page. Uses bengal.utils.text.slugify for tag slug generation.
1 str
def tag_url(tag: str) -> str

Generate URL for a tag page.

Uses bengal.utils.text.slugify for tag slug generation.

Parameters 1

Name Type Default Description
tag str

Tag name

Returns

str

URL path to tag page

has_tag
Check if page has a specific tag.
2 bool
def has_tag(page: Any, tag: str) -> bool

Check if page has a specific tag.

Parameters 2

Name Type Default Description
page Any

Page to check

tag str

Tag to look for

Returns

bool

True if page has the tag