Skip to main content

Crate texl_core

Crate texl_core 

Source
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::render renders a template in a given Scope. It is the low-level entry point.
  • Engine adds what a full application needs: globals shared by every template, loaders that fetch documents by URI, texl.import with relative URIs, and a cache.
  • helpers connects 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.

Re-exports§

pub use engine::Engine;
pub use engine::LoaderFn;
pub use engine::loader_fn;

Modules§

engine
The texl engine: documents, loaders and imports.
helpers
Glue between Swan promises and the host’s async runtime.
template
Template rendering for Texl.