Skip to main content

Module template

Module template 

Source
Expand description

Template rendering for Texl.

A template is plain text containing Swan expressions delimited by <% and %>. render replaces each <% expr %> tag with the text form of the value that expr evaluates to, and copies all other text through unchanged.

§Syntax

  • A tag starts at <% and ends at the first following %>. Tags cannot be nested, and the sequence %> cannot appear inside an expression.
  • If a <% has no matching %>, rendering stops at that point and the rest of the template, including the stray <%, is copied through literally.

§Evaluation model

  • Tags are evaluated strictly in order, all against the same Scope. Assignments made in one tag are therefore visible in later tags (e.g. <% count = 1 %>Count is <% count %>).
  • If an expression yields a Value::Promise, rendering suspends until the promise resolves (repeating if the result is itself a promise) and then renders the resolved value. This is why render is async.
  • The future is single-threaded: it holds Rc values and is not Send.

§Output format

ValueOutput
Textthe text itself
NumbRust f64 display (30, not 30.0; 1.5)
BoolTRUE / FALSE
Nothingempty text
Tuplethe items, rendered and joined with ", "
List[[List of N]]
Namespace[[Namespace of N]] (N = number of local bindings)
Func[[Func]]
Undefined[[Undefined]]
Promise (resolved)the rendered resolved value
Promise (pending)[[Pending Promise]]

Promises returned by a tag are awaited, so [[Pending Promise]] only appears for promises nested inside other values, such as tuple items.

§Example

use swan_core::builtins::root_scope;
use swan_core::value::Value;
use texl_core::template::render;

let scope = root_scope();
scope.set("name", Value::Text("World".into()));
let out = block_on(render("Hello <% name %>!", &scope));
assert_eq!(out, "Hello World!");

Functions§

render
Renders template, evaluating every <% ... %> expression against scope.