Dead-simple string templating.
Leon parses a template string into a list of tokens, and then substitutes
provided values in. Unlike other templating engines, it is extremely simple:
it supports no logic, only replaces. It is even simpler than format!(),
albeit with a similar syntax.
Syntax
it is better to rule { group }
one can live {adverb} without power
A replacement is denoted by { and }. The contents of the braces, trimmed
of any whitespace, are the key. Any text outside of braces is left as-is.
To escape a brace, use \{ or \}. To escape a backslash, use \\. Keys
cannot contain escapes.
\{ leon \}
The above examples, given the values group = "no one" and
adverb = "honourably", would render to:
it is better to rule no one
one can live honourably without power
{ leon }
Usage
A template is first parsed to a token list:
use Template;
let template = parse.unwrap;
The template can be inspected, for example to check if a key is present:
# use Template;
#
# let template = parse.unwrap;
assert!;
The template can be rendered to a string:
# use Template;
use vals;
#
# let template = parse.unwrap;
assert_eq!;
…or to a writer:
use Write;
# use Template;
use vals;
#
# let template = parse.unwrap;
let mut buf: = Vecnew;
template.render_into.unwrap;
assert_eq!;
…with a map:
use HashMap;
# use Template;
# let template = parse.unwrap;
let mut values = new;
values.insert;
assert_eq!;
…or with your own type, if you implement the [Values] trait:
# use Template;
use Cow;
use Values;
#
# let template = parse.unwrap;
let values = MyMap ;
assert_eq!;
Compile-time parsing
You can either use leon-macros's
template!,
a proc-macro, with the exact same syntax as the normal parser, or this
crate's [template!] rules-macro, which requires a slightly different
syntax but doesn't bring in additional dependencies. In either case,
the leon library is required as a runtime dependency.
Errors
Leon will return a [ParseError] if the template fails to
parse. This can happen if there are unbalanced braces, or if a key is empty.
Leon will return a [RenderError::MissingKey] if a key is missing from keyed
values passed to [Template::render()], unless a default value is provided
with [Template.default].
It will also pass through I/O errors when using [Template::render_into()].