Simple Customizable String-Templating Library for Rust.
This Library is inspired by string_template
Add this to your Cargo.toml
(or use cargo-add
):
toml
[dependencies]
new_string_template = "1.3"
Example with 2 data points (with fail enabled):
```rust use newstringtemplate::template::Template; use std::collections::HashMap;
fn main() { let templstr = "Something {data1} be {data2}, and { not here }"; let templ = Template::new(templstr); let data = { let mut map = HashMap::new(); map.insert("data1", "should"); map.insert("data2", "here"); map };
let rendered = templ.render(&data).expect("Expected Result to be Ok");
assert_eq!("Something should be here, and { not here }", rendered);
} ```
Example with 1 data point (with fail disabled):
```rust use newstringtemplate::template::Template; use std::collections::HashMap;
fn main() { let templstr = "Something {data1} be {data2}, and { not here }"; let templ = Template::new(templstr); let data = { let mut map = HashMap::new(); map.insert("data1", "should"); // map.insert("data2", "here"); map };
let rendered = templ.render_nofail(&data);
assert_eq!("Something should be {data2}, and { not here }", rendered);
} ```
Example with Custom Regex:
```rust use newstringtemplate::template::Template; use std::collections::HashMap; use regex::Regex;
fn main() { // The following regex requires at least one space between "{{" and "}}" and allows variables with spaces let customregex = Regex::new(r"(?mi){{\s+([^}]+)\s+}}").unwrap(); let templstr = "Something {{ data1 }} be {{ data2 }}, and {{ data 3 }}"; let templ = Template::new(templstr).withregex(&custom_regex); let data = { let mut map = HashMap::new(); map.insert("data1", "should"); map.insert("data2", "here"); map.insert("data 3", "here too"); map };
let rendered = templ.render_nofail(&data);
assert_eq!("Something should be here, and here too", rendered);
} ```
Note: with the default regex, an template-variable can have spaces or none at all.
This project requires:
- NodeJS with yarn
installed (when working on an main branch)
- Rust install with rustfmt
& clippy