Expand description
The engine::Engine struct: template parser, expression
evaluator, partial loader, inheritance resolver, filter pipeline,
and renderer for both in-memory strings and .html files on disk.
§Engine Module
The template rendering engine. The Engine struct holds the
template root directory, the render cache, and the active delimiter
pair, and exposes Engine::render_template (string-in, string-out)
and Engine::render_page (read a .html layout from disk).
§Templating language
- Substitution:
{{key}}looks up aContextvalue and emits it. Default behaviour HTML-escapes& < > " '. Use{{!key}}to emit the value verbatim, orEngine::with_html_escape(false)to disable escape globally. - Dot-notation:
{{user.email}}walks aValue::Map;{{items.0}}indexes aValue::List. - Control flow:
{{#if EXPR}}…{{else}}…{{/if}}and{{#each list}}…{{/each}}. Each-blocks expose the loop helpers@index,@first,@last, and (forMapiteration)@key, binding each element tothis. - Expressions (inside
#if): comparisons (==,!=,<,<=,>,>=), short-circuiting boolean operators (and,or,not), checked integer math (+,-,*,/), and postfix tests (is defined,is empty,is none) withis notfor negation. Precedence: postfix tests bind tightest, then math, comparisons,not,and,or. Bare paths like{{#if user}}keep their truthiness semantics. - Partials:
{{> name}}readsname.htmlfromtemplate_pathand substitutes the parent context. Pass scoped parameters via{{> name k=v}}. Recursion is capped at depth 10. - Inheritance:
{{#extends "base"}}plus{{#block "name"}}…{{/block}}lets a child template override named blocks in its parent. Multi-level chains compose; the child wins on conflicting block names. - In-template assignment:
{{#set name = LITERAL}}binds a value locally for subsequent tags. Local-scope only — does not leak into the parent context. - Filters:
{{ x | filter }}, with arguments via{{ x | filter:arg }}. Built-in filters:uppercase,lowercase,trim,truncate,capitalize,length,default,replace,urlencode,safe. - Comments:
{{! one-line }}and{{!-- multi-line --}}are stripped before rendering. - Whitespace control:
{{- key -}}trims adjacent whitespace on the corresponding side of the tag. - Backslash escape:
\{{literal}}emits{{literal}}verbatim. Even runs collapse to literal backslashes, odd runs escape the following delimiter. - Custom delimiters:
Engine::set_delimiters("<<", ">>")swaps{{/}}for any pair.
§Caching
Engine::render_page caches results keyed by
"{layout}:{Context::hash()}". The cache uses true LRU eviction
when bounded — see crate::cache.
§Errors
Both render entry points return Result<String, EngineError>.
Missing keys produce EngineError::Render; malformed templates
produce EngineError::InvalidTemplate. See crate::error.
§Examples
use staticweaver::{Context, Engine};
use std::time::Duration;
let engine = Engine::new("templates", Duration::from_secs(60));
let mut ctx = Context::new();
ctx.set("name".to_string(), "Ada".to_string());
let out = engine
.render_template("Hello, {{name}}!", &ctx)
.unwrap();
assert_eq!(out, "Hello, Ada!");Structs§
- Engine
- The main template rendering engine.
- FsLoader
- Filesystem-backed
TemplateLoader. Resolvesnameto<root>/<name>.html. Used as the default loader byEngine::new. - Memory
Loader - In-memory
TemplateLoaderbacked by aHashMap. Useful for tests, embedded asset bundles, or any case where templates are known at build time and shouldn’t touch the filesystem.
Enums§
- Engine
Error - Canonical engine error type. Re-exported from
crate::errorto keep a single source of truth; callers can use eitherstaticweaver::EngineErrororstaticweaver::engine::EngineErrorand pattern-match interchangeably.
Constants§
- DEFAULT_
TEMPLATE_ FILES - Filenames fetched by default when
Engine::create_template_folderis called with an HTTP/S URL and no explicit file list. Matches the historical six-file set for backwards compatibility; callers who need a different layout should useEngine::create_template_folder_with_files.
Traits§
- Template
Loader - Filter, test and loader extension points. Defined in
staticweaver-coreand re-exported here at their historical paths. Source-of-truth for template files.
Type Aliases§
- Filter
Fn - Filter, test and loader extension points. Defined in
staticweaver-coreand re-exported here at their historical paths. Signature of a user-registered filter, as accepted bystaticweaver::Engine::add_filter. The filter receives the current pipeline value as&strand any colon-separated arguments as&[String], and returns the transformed value or anEngineError. Wrapped in anArcso an engine stays cheap to clone. - TestFn
- Filter, test and loader extension points. Defined in
staticweaver-coreand re-exported here at their historical paths. Signature of a user-registered test, as accepted bystaticweaver::Engine::add_test. The test receives the operand value theis Xpredicate is applied to and any extra arguments, and returns a boolean (or anEngineErrorfor malformed inputs). Custom tests override built-ins (defined,empty,none) of the same name.