Expand description
The texl template engine.
A texl template is text with embedded <% ... %> tags. Each tag contains an
expression in Swan, and rendering replaces the tag with the
value of its expression:
Hello <% name %>, you have <% list.size messages %> new messages.The Texl manual explains how to write templates. This documentation covers the Rust API.
§Overview
template::renderrenders a template in a givenScope. It is the low-level entry point.Engineadds what a full application needs: globals shared by every template, loaders that fetch documents by URI,texl.importwith relative URIs, and a cache.helpersconnects the engine to the host’s async runtime.
§Async rendering
Rendering is async because a tag may return a Swan promise, for example
the result of texl.import, and rendering waits for it. The futures are
single-threaded (!Send). With tokio, run them on a LocalSet and pass a
spawner that calls tokio::task::spawn_local:
ⓘ
use texl_core::{Engine, helpers::spawner_fn};
let engine = Engine::new(Some(spawner_fn(|task| { tokio::task::spawn_local(task); })));
let text = tokio::task::LocalSet::new().run_until(engine.render("<% 6 * 7 %>")).await;Without a runtime, pass None and drive the futures yourself, calling
helpers::run_pending to run the background tasks.