Skip to main content

Module snippet

Module snippet 

Source
Expand description

Library snippets — reusable, non-page content addressable by slug.

Snippets are the bridge between Library types (the trait-shape registrations from crate::library) and live pages: editors create a snippet under a given type, give it a slug, and any template can pull it back via {{ cms_snippet(slug="hero-cta") }}.

Storage uses one generic cms_snippet table (rather than per-type extension tables) so adding a new LibraryTypeHandler doesn’t require migrating tables — typed fields go in the data JSON column when the registered type wants more than title + body.

The slug is unique tenant-wide so cms_snippet(slug="hero-cta") resolves without needing the caller to know the type. Pass type_name= when slugs collide across types.

Structs§

Snippet
A single reusable content block. One row per snippet instance.
SnippetRevision
SnippetsGuard
Clears the installed snippet maps on drop. Returned by install.

Functions§

capture_revision
Capture one revision row for snippet. Bumps sequence off the current max for that snippet id. Idempotent in the sense that repeated calls produce one row per call — callers should only invoke after a successful save.
child_folders
All direct child folders of parent — walks every snippet’s folder_path, keeps only the segment immediately under parent, dedupes + sorts. Empty parent means root.
folder_tree_for_type
Every folder path used by snippets of type_name, plus every intermediate folder derived from the leaf paths. Result is sorted and includes the empty (root) folder.
install
Install the render’s prefetched snippets (from prefetch_all) for cms_snippet(slug=…), until the guard drops.
normalize_folder
Normalize a user-typed folder path into the storage form: no leading slash, single trailing slash on non-empty paths, collapsed runs of slashes, and segment trim. Empty input (or /) → root ("").
prefetch_all
Prefetch every snippet for the current tenant and return two slug → html maps (wrapped + inline). The public-render flow inserts them into the Tera context so {{ cms_snippet(slug=…) }} resolves without re-entering the DB.
register_tera_function
Register the cms_snippet Tera function. Wired from crate::urls::register_tera_helpers.
revision_by_sequence
Fetch one revision by (snippet_id, sequence). Returns None when no matching row exists.
revisions_for
Every revision row for snippet_id, newest first.
set_element_template
Write (or clear) an element’s own template name in its data.