std.template

Lightweight key-based text templates.

Importuse std.template

Types

struct Pair#

Key/value pair for std.template rendering.

key: string

Placeholder name, written {{key}} in the template.

value: string

Text substituted for it.

Functions

fn pair(key: string, value: string) -> Pair#

A Pair for render's substitution list.

  • key: Placeholder name, without the braces.
  • value: Text to substitute for it.
fn render(template: string, pairs: [Pair]) -> string#

Replaces every {{key}} with its pair's value; unknown placeholders stay.

  • template: Text carrying {{key}} placeholders.
  • pairs: Substitutions to apply; placeholders with no pair are left as written.
let text = std.template.render(
  "Hello {{name}}, welcome to {{place}}!",
  [std.template.pair("name", "Ada"), std.template.pair("place", "moon")],
)
println(text)                                // Hello Ada, welcome to moon!
println(std.template.render("Hi {{who}}", [])) // Hi {{who}}
fn render_lines(template: string, pairs: [Pair]) -> string#

render, line-oriented alias (identical substitution semantics).

  • template: Text carrying {{key}} placeholders.
  • pairs: Substitutions to apply.
fn render_if(template: string, condition: bool, pairs: [Pair]) -> string#

render(template, pairs) when condition holds, else the empty string.

  • template: Text carrying {{key}} placeholders.
  • condition: When false, nothing is rendered at all.
  • pairs: Substitutions to apply.
let pairs = [std.template.pair("n", "3")]
println(std.template.render_if("[{{n}} new]", true, pairs))        // [3 new]
println(std.template.render_if("[{{n}} new]", false, pairs) == "") // true
fn render_each(template: string, placeholder: string, values: [string]) -> string#

Concatenates one rendering of template per value, substituting {{placeholder}} each time.

  • template: Text rendered once per value.
  • placeholder: Placeholder name each value is substituted for.
  • values: Values to render, in order.
let list = std.template.render_each("<li>{{item}}</li>", "item", ["A", "B"])
println(list) // <li>A</li><li>B</li>