Expand description
auto_menu(parent_id=…, depth=…) Tera function — an automatic
menu built from the page tree.
The function walks the page tree at render time,
filters by show_in_menus = True + status = "published", and
returns a nested list a host template can iterate to emit nav
chrome. Hosts don’t have
to hand-roll the SQL or maintain a cms_navigation row when the
menu IS the published tree.
§Usage
{% set items = auto_menu(parent_id=1, depth=2) %}
<nav>
{% for it in items %}
<a href="{{ it.url_path }}">{{ it.title }}</a>
{% if it.children | length > 0 %}
<ul>
{% for c in it.children %}
<li><a href="{{ c.url_path }}">{{ c.title }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</nav>§Arguments
parent_id— id of the page whose immediate children seed the menu. Omit /0/nullfor tree roots (parent_id IS NULL).depth— how many levels of children to expand.1= direct children only (eachchildrenlist empty).2= include grandchildren. Default1.
Each emitted item has page_id, title, url_path, and a
children list of the same shape.
§Implementation
Same thread-local install pattern as crate::page_url /
crate::children_filtered — the public-render handler
pre-fetches every show_in_menus = true page the resolver would
serve (published, archived, or scheduled and past go-live) in one
query, installs a slim projection in a thread-local,
the function builds the nested tree from the in-memory list at
call time. The set is bounded (typically tens; full sites a few
hundred), so one fetch per render amortizes across N
auto_menu() calls in the same template.
Structs§
- Auto
Menu Guard - RAII guard. While in scope
auto_menu(...)resolves against the installed rows; on drop the thread-local clears so a follow-up render on the same tokio worker doesn’t inherit stale data. - Menu
Item - JSON-friendly shape templates iterate over.
- MenuRow
- Slim projection of a
Pagerow carrying just the columns the menu builder needs. Drops everything else (theme, og_*, SEO body, etc.) so the per-render thread-local stays small.
Constants§
- DEFAULT_
DEPTH - Default depth when the template omits the kwarg — direct children only.
Functions§
- install
- Install the menu row pool for the duration of the returned guard.
- prefetch
- Fetch every published page with
show_in_menus = true, project toMenuRow, and return the pool. Bounded by the tenant’s menu-eligible cardinality (typically tens; a large site a few hundred). Failures degrade to an empty pool — the menu just renders empty rather than 500’ing the page. - prefetch_
cached - Cached variant of
prefetch. Serves the menu pool fromcacheunderkey, recomputing + storing on miss viarustango::cache_fragment::cached_render. The pool is a slim projection of the published, menu-eligible tree; it changes only when a page’sshow_in_menus/ title / slug / order changes, so a shortttlbounds staleness exactly like the public page cache. - register_
tera_ function - Register the
auto_menuTera function. Wired fromcrate::urls::register_tera_helpers.