Introduction
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
.
Features
- Parse the template from a
str
that's easy to write, - Support for alternatives in case some variables are not present,
Use
?
to separate the alternatives, uses whichever it can find first. If?
is at the end, leaves it blank instead of erroring out. - Support for literal strings inside the alternative options,
You can use a literal string
"string"
enclosed in"
as an alternative if you want to put something instead of blank at the end. - Support for the date time format using
chrono
, You can use any format starting with%
inside the variable placeholder{}
to use a date time format supported by chrono. - Support for any arbitrary commands, etc.
You can keep any command inside
$(
and)
to run it and use the result in the template. You can use other format elements inside it.
Usages
Simple variables:
let templ = parse_template.unwrap;
let mut vars: = new;
vars.insert;
let rendered = templ
.render
.unwrap;
assert_eq!;
Safe replace, blank if not present, or literal string if not present:
let templ = parse_template.unwrap;
let vars: = new;
let rendered = templ
.render
.unwrap;
assert_eq!;
Alternate, whichever variable it finds first will be replaced:
let templ = parse_template.unwrap;
let mut vars: = new;
vars.insert;
let rendered = templ
.render
.unwrap;
assert_eq!;
Custom Commands:
let templ = parse_template.unwrap;
let mut vars: = new;
vars.insert;
let rendered = templ
.render
.unwrap;
assert_eq!;
You can turn off Custom Commands for safety:
let templ = parse_template.unwrap;
let mut vars: = new;
vars.insert;
let rendered = templ
.render
.unwrap;
assert_eq!;
Date Time:
let templ = parse_template.unwrap;
let timefmt = now.format;
let output = format!;
let mut vars: = new;
vars.insert;
let rendered = templ
.render
.unwrap;
assert_eq!;
Render Iter
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.
let templ = parse_template.unwrap;
let mut vars: = new;
vars.insert;
let options = RenderOptions ;
let mut names = options.render_iter;
assert_eq!;
assert_eq!;
assert_eq!;
Limitations
- You cannot use positional arguments in this template system, only named ones.
{}
will be replaced with empty string. Although you can use"0"
,"1"
, etc as variable names in the template and the render options variables. - I haven't tested variety of names, although they should work try to keep the names identifier friendly.
- Currently doesn't have format specifiers, for now you can use the command options with
printf
bash command to format things the way you want. Like a templatethis is $(printf "%.2f" {weight}) kg.
should be rendered with the correct float formatting.