Module

rendering.template_engine.url_helpers

URL generation helpers for template engine.

Provides URL generation functions for pages and assets with baseurl support.

URL NAMING CONVENTION:

====================== Bengal uses explicit naming to prevent path/URL confusion across the codebase:

  • _path: Site-relative path WITHOUT baseurl (internal use only) Example: "/docs/getting-started/" Use for: Internal lookups, comparisons, active trail detection, caching Note: Underscore prefix signals "internal only" to AI assistants

  • href: Public URL WITH baseurl applied (for templates) Example: "/bengal/docs/getting-started/" (when baseurl="/bengal") Use for: Template href attributes, external links

TEMPLATE USAGE:


In templates, always use .href for href attributes:

{{ page.title }} {# Correct #} {{ item.title }} {# Correct #}

The .href property automatically includes baseurl when configured.

HELPER FUNCTIONS:


  • href_for(obj): Get public URL for any page-like object (preferred)
  • apply_baseurl(path, site): Use bengal.rendering.utils.url.apply_baseurl

Related Modules:

  • bengal.rendering.utils.url: Canonical location for apply_baseurl
  • bengal.core.nav_tree: NavNodeProxy provides .href (with baseurl) and ._path (without)
  • bengal.core.page: Page.href includes baseurl, Page._path does not
  • bengal.core.section: Same pattern as Page

Functions

href_for 2 str
Get href for any object. Prefer obj.href directly. This is the recommended way…
def href_for(obj: PageLike | Any, site: Any = None) -> str

Get href for any object. Prefer obj.href directly.

This is the recommended way to get URLs in template functions and filters. The returned URL includes baseurl and is ready for use in href attributes.

Parameters
Name Type Description
obj PageLike | Any

Page, Section, Asset, NavNodeProxy, or dict-like object with href/_path

site Any

Unused, kept for backward compatibility

Default:None
Returns
str
filter_dateformat 2 str
Format a date using strftime.
def filter_dateformat(date: datetime | str | None, format: str = '%Y-%m-%d') -> str
Parameters
Name Type Description
date datetime | str | None

Date to format

format str

strftime format string

Default:'%Y-%m-%d'
Returns
str