Skip to main content

Module auto_menu

Module auto_menu 

Source
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 / null for tree roots (parent_id IS NULL).
  • depth — how many levels of children to expand. 1 = direct children only (each children list empty). 2 = include grandchildren. Default 1.

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§

AutoMenuGuard
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.
MenuItem
JSON-friendly shape templates iterate over.
MenuRow
Slim projection of a Page row 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 to MenuRow, 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 from cache under key, recomputing + storing on miss via rustango::cache_fragment::cached_render. The pool is a slim projection of the published, menu-eligible tree; it changes only when a page’s show_in_menus / title / slug / order changes, so a short ttl bounds staleness exactly like the public page cache.
register_tera_function
Register the auto_menu Tera function. Wired from crate::urls::register_tera_helpers.