Template with support for optional variables and such.
This is a simple template tool that works with variable names and
HashMap
of String
. The Template
can be parsed from
str
and then you can render it using the variables in
HashMap
and any shell commands running through Exec
.
str
that's easy to write,?
to separate the alternatives, uses whichever it can find first. If ?
is at the end, leaves it blank instead of erroring out."string"
enclosed in "
as an alternative if you want to put something instead of blank at the end.chrono
,
You can use any format starting with %
inside the variable placeholder {}
to use a date time format supported by chrono.$(
and )
to run it and use the result in the template. You can use other format elements inside it.printf
bash command to format things the way you want.
Like a template this is $(printf "%.2f" {weight}) kg.
should be rendered with the correct float formatting.
let templ = parse_template("hello {nickname?name}. You're $(printf \"%.1f\" {weight})kg").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("name".into(), "John".into());
vars.insert("weight".into(), "132.3423".into());
let rendered = templ
.render(&RenderOptions {
wd: PathBuf::from("."),
variables: vars,
})
.unwrap();
assert_eq!(rendered, "hello John. You're 132.3kg");