Expand description
Temple is a small language for shaping data: an input value goes in, a value of the shape you asked for comes out.
let total = input.items.map(i -> i.price * i.qty).sum()
{
"customer": {{ input.customer.name }},
"total": {{ total }},
"vip": {{ total > 1000 }}
}§Rendering a template
Template::compile parses and checks the source once; render it as
many times as you like, into a Value or straight into your own type.
use serde::Deserialize;
use temple_dsl::{Template, Value};
#[derive(Deserialize)]
struct Greeting {
text: String,
}
let template = Template::compile(r#"{ "text": {{ concat("hi ", input.name) }} }"#).unwrap();
let greeting: Greeting = template.render(Value::obj([("name", "Ada")])).unwrap();
assert_eq!(greeting.text, "hi Ada");§Embedding temple in another language
A host parses one expression or value at a time inside its own syntax
(ExprUnit::parse_at, TemplateUnit::parse_at), checks it against
the Names it will bind, and evaluates it with an Env under a
Budget.
use temple_dsl::{Budget, Env, ExprUnit, Names, Value};
let names = Names::new().var("limit");
let unit = ExprUnit::compile("input.age >= limit", &names).unwrap();
let env = Env::new().set("limit", 18);
let out = unit
.eval(&Value::obj([("age", Value::Int(21))]), &env, &Budget::new(1_000))
.unwrap();
assert_eq!(out, Value::Bool(true));Structs§
- Budget
- A work limit shared by every unit evaluated with it: one unit per expression and per element a method or built-in touches; copies cost one per element and one per 64 bytes of text.
- Env
- The values, and optionally the functions, a unit is evaluated with.
- Expr
Unit - A checked expression, ready to evaluate with an
Env. - Functions
- Host functions written in temple, callable from units evaluated with an
Envthat carries them. - Input
Read - A read of an
inputpath:input.data.orderis["data", "order"], stopping at the first index or method call. - Names
- The variables and functions a unit may use besides
inputandthis. - Parsed
Expr - An expression parsed out of a larger source, not yet checked.
- Parsed
Template - An output value parsed out of a larger source, not yet checked.
- Span
- A byte range
start..endin a source string. - Template
- A compiled template.
- Template
Unit - A checked output value, ready to render with an
Env.
Enums§
- Compile
Error - Why a template could not be compiled.
- Eval
Error - Why evaluating a unit failed.
- Load
Error - Why a stored template could not be loaded.
- Render
Error - Why a template could not be rendered.
- Value
- A dynamic value: a render’s input and, unless deserialized into your own type, its output. Numbers are integers or exact decimals, never floats; objects keep insertion order.
Constants§
- BLOB_
VERSION - Version of the blob format
Template::to_byteswrites. - VERSION
- This crate’s version, for hosts that record it next to stored blobs.
Functions§
- is_
builtin_ function - Whether
nameis a built-in function, and so can’t name a host function. - is_
reserved_ name - Whether
nameis reserved (input,this,let, …) and so can’t name a variable or function.