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.Simple variables:
rust
let templ = parse_template("hello {name}").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("name".into(), "world".into());
let rendered = templ
.render(&RenderOptions {
variables: vars,
..Default::default()
})
.unwrap();
assert_eq!(rendered, "hello world");
Safe replace, blank if not present, or literal string if not present:
rust
let templ = parse_template("hello {name?} {lastname?\"User\"}").unwrap();
let vars: HashMap<String, String> = HashMap::new();
let rendered = templ
.render(&RenderOptions {
variables: vars,
..Default::default()
})
.unwrap();
assert_eq!(rendered, "hello User");
Alternate, whichever variable it finds first will be replaced:
rust
let templ = parse_template("hello {nickname?name}").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("name".into(), "world".into());
let rendered = templ
.render(&RenderOptions {
variables: vars,
..Default::default()
})
.unwrap();
assert_eq!(rendered, "hello world");
Custom Commands:
rust
let templ = parse_template("L=$(printf \"%.2f\" {length})").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("length".into(), "12.342323".into());
let rendered = templ
.render(&RenderOptions {
wd: PathBuf::from("."),
variables: vars,
shell_commands: true,
})
.unwrap();
assert_eq!(rendered, "L=12.34");
You can turn off Custom Commands for safety:
rust
let templ = parse_template("L=$(printf \"%.2f\" {length})").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("length".into(), "12.342323".into());
let rendered = templ
.render(&RenderOptions {
wd: PathBuf::from("."),
variables: vars,
shell_commands: false,
})
.unwrap();
assert_eq!(rendered, "L=$(printf \"%.2f\" 12.342323)");
Date Time:
rust
let templ = parse_template("hello {name} at {%Y-%m-%d}").unwrap();
let timefmt = Local::now().format("%Y-%m-%d");
let output = format!("hello world at {}", timefmt);
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("name".into(), "world".into());
let rendered = templ
.render(&RenderOptions {
wd: PathBuf::from("."),
variables: vars,
shell_commands: false,
})
.unwrap();
assert_eq!(rendered, output);
Makes a RenderIter<'a>
that can generate incremented strings from the given Template
and the RenderOptions
. The Iterator will have -N
appended where N is the number representing the number of instance.
rust
let templ = parse_template("hello {name}").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("name".into(), "world".into());
let options = RenderOptions {
variables: vars,
..Default::default()
};
let mut names = options.render_iter(&templ);
assert_eq!("hello world-1", names.next().unwrap());
assert_eq!("hello world-2", names.next().unwrap());
assert_eq!("hello world-3", names.next().unwrap());
{}
will be replaced with empty string. Although you can use "0"
, "1"
, etc as variable names in the template and the render options variables.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.