Skip to main content

TemplateLoader

Trait TemplateLoader 

Source
pub trait TemplateLoader: Send + Sync {
    // Required method
    fn load(&self, name: &str) -> Result<String, EngineError>;
}
Expand description

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

The facade crate’s FsLoader reads templates from a directory on disk, mapping name to <root>/<name>.html. Implementing this trait yourself lets you serve templates from any backend: an in-memory map (a BTreeMap<String, String> implements it), a database, an embedded asset bundle, or a remote service.

Loaders are looked up whenever the renderer resolves a {{> partial}}, a {{#extends "base"}}, or a named render. The name passed in has already been validated against path-traversal patterns (see crate::path::validate_name), so implementations don’t need to re-validate.

Send + Sync is required so an engine stays usable across threads, e.g. behind an Arc in a web handler.

§Examples

use staticweaver_core::TemplateLoader;
use std::collections::BTreeMap;

let mut loader = BTreeMap::new();
let _ = loader.insert("hello".to_string(), "Hi {{name}}!".to_string());
assert_eq!(loader.load("hello").unwrap(), "Hi {{name}}!");
assert!(loader.load("missing").is_err());

Required Methods§

Source

fn load(&self, name: &str) -> Result<String, EngineError>

Loads the template named name. Implementations should return EngineError::Io (with the std feature) or EngineError::ResourceNotFound for missing or unreadable templates.

§Errors

Returns an error when no template named name exists or it cannot be read.

§Examples
use staticweaver_core::TemplateLoader;
use std::collections::BTreeMap;

let loader: BTreeMap<String, String> = BTreeMap::new();
assert!(loader.load("missing").is_err());

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl TemplateLoader for BTreeMap<String, String>

Source§

fn load(&self, name: &str) -> Result<String, EngineError>

Implementors§