Skip to main content

Module snippet_render

Module snippet_render 

Source
Expand description

snippet_html(type=…, id=N) Tera helper — per-snippet template rendering.

Pairs with crate::library::LibraryTypeHandler::render_template: a page template asks for a row of such a type by (type, id) and gets it rendered through that template.

A type may instead let each element name its own template, by returning true from allows_element_template; the name then comes from that row’s data.template and falls back to the type’s own. That is what makes an element the whole unit — its content and its markup travel together — and it needs no migration, because cms_snippet.data is already free-form JSON.

The name resolves against the Tera instance handed to fill, which the public router has already overlaid with the tenant’s templates (router.rs tera_for). So a template written into templates_tenants/<org>/… — by the admin editor or the write_template MCP tool — is renderable by name here, with no per-render add_raw_template.

Rendering is lazy and scoped (#731). While the page renders, each snippet_html(...) call is recorded and leaves a marker; afterwards fill fetches only the recorded rows — one query per type — renders them and puts the HTML in place of the markers. A page pays for the snippets it shows, not for every row of every templated type.

§Usage in templates

{{ snippet_html(type="Category", id=4) | safe }}

Unknown type, unknown id, or a type without a template → empty. Use the call’s output as it is: the marker is replaced in the finished page, so a filter applied to it (| escape, | truncate) mangles the marker instead of the snippet.

Structs§

SnippetRecorder
RAII guard: while in scope, snippet_html(...) records its key and returns a marker. SnippetRecorder::take ends the recording.

Functions§

fill
Render the keys a page recorded and put each snippet’s HTML in place of its marker in html. Snippets whose own templates call snippet_html are filled the same way, up to [MAX_NESTING] levels.
record
Start recording snippet_html(...) calls on this thread. Bind it right before tera.render, with no .await in between.
register_tera_function
Register the snippet_html Tera function. Wired from crate::urls::register_tera_helpers.