Skip to main content

Module engine

Module engine 

Source
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 a Context value and emits it. Default behaviour HTML-escapes & < > " '. Use {{!key}} to emit the value verbatim, or Engine::with_html_escape(false) to disable escape globally.
  • Dot-notation: {{user.email}} walks a Value::Map; {{items.0}} indexes a Value::List.
  • Control flow: {{#if EXPR}}…{{else}}…{{/if}} and {{#each list}}…{{/each}}. Each-blocks expose the loop helpers @index, @first, @last, and (for Map iteration) @key, binding each element to this.
  • Expressions (inside #if): comparisons (==, !=, <, <=, >, >=), short-circuiting boolean operators (and, or, not), checked integer math (+, -, *, /), and postfix tests (is defined, is empty, is none) with is not for negation. Precedence: postfix tests bind tightest, then math, comparisons, not, and, or. Bare paths like {{#if user}} keep their truthiness semantics.
  • Partials: {{> name}} reads name.html from template_path and 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. Resolves name to <root>/<name>.html. Used as the default loader by Engine::new.
MemoryLoader
In-memory TemplateLoader backed by a HashMap. Useful for tests, embedded asset bundles, or any case where templates are known at build time and shouldn’t touch the filesystem.

Enums§

EngineError
Canonical engine error type. Re-exported from crate::error to keep a single source of truth; callers can use either staticweaver::EngineError or staticweaver::engine::EngineError and pattern-match interchangeably.

Constants§

DEFAULT_TEMPLATE_FILES
Filenames fetched by default when Engine::create_template_folder is 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 use Engine::create_template_folder_with_files.

Traits§

TemplateLoader
Filter, test and loader extension points. Defined in staticweaver-core and re-exported here at their historical paths. Source-of-truth for template files.

Type Aliases§

FilterFn
Filter, test and loader extension points. Defined in staticweaver-core and re-exported here at their historical paths. Signature of a user-registered filter, as accepted by staticweaver::Engine::add_filter. The filter receives the current pipeline value as &str and any colon-separated arguments as &[String], and returns the transformed value or an EngineError. Wrapped in an Arc so an engine stays cheap to clone.
TestFn
Filter, test and loader extension points. Defined in staticweaver-core and re-exported here at their historical paths. Signature of a user-registered test, as accepted by staticweaver::Engine::add_test. The test receives the operand value the is X predicate is applied to and any extra arguments, and returns a boolean (or an EngineError for malformed inputs). Custom tests override built-ins (defined, empty, none) of the same name.