strings::template
This module provides support for formatting of large or complex strings beyond
the scope of fmt. A template is compiled using compile, then executed
with execute to print formatted text to an io::handle.
The template format is a string with variables substituted using "$". Variable
names must be alphanumeric ASCII characters (i.e. for which ascii::isalnum
returns true). A literal "$" may be printed by using it twice: "$$". Variables
may also be used with braces, i.e. ${variable}, so that they can be placed
immediately next to alphanumeric characters; such variables may include
non-alphanumeric characters other than '{' and '}'.
const src = "Hello, $user! Your balance is $$$balance.\n";
const template = template::compile(src)!;
defer template::finish(&template);
template::execute(&template, os::stdout,
("user", "ddevault"),
("balance", 1000),
)!; // "Hello, ddevault! Your balance is $1000.
Index
Types
type param;
type instruction;
type literal;
type template;
type variable;
Functions
fn compile(str) (template | errors::invalid);
fn execute(*template, io::handle, param...) (size | io::error);
fn finish(*template) void;
Types
type param
type param = (str, fmt::formattable);
Parameters to execute with a template, a tuple of a variable name and a
formattable object.
type instruction
Show undocumented member
type instruction = (literal | variable);
type literal
Show undocumented member
type literal = str;
type template
Show undocumented member
type template = []instruction;
type variable
Show undocumented member
type variable = str;
Functions
fn compile
fn compile(input: str) (template | errors::invalid);
Compiles a template string. The return value must be freed with finish
after use.
fn execute
fn execute(tmpl: *template, out: io::handle, params: param...) (size | io::error);
Executes a template, writing the output to the given io::handle. If the
template calls for a parameter which is not provided, an assertion will be
fired.
fn finish
fn finish(tmpl: *template) void;
Frees resources associated with a template.