Struct new_string_template::template::Template
source · [−]pub struct Template { /* private fields */ }Expand description
Struct to store the template
Implementations
Create a new Template Instance with the default regex.
Example
let input_template = "Some {{ Template }}";
let template_instance = Template::new(input_template);Render the template with the provided values.
This function takes a HashMap where the key is str.
Errors
This function Errors on the first problem encountered
Example
let templ_str = "Something {data1} be {data2}, and { not here }";
let templ = Template::new(templ_str);
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);pub fn render_string<T: AsRef<str>>(
&self,
values: &HashMap<String, T>
) -> Result<String, TemplateError>
pub fn render_string<T: AsRef<str>>(
&self,
values: &HashMap<String, T>
) -> Result<String, TemplateError>
Render the template with the provided values.
This function takes a HashMap where the key is String.
Errors
This function Errors on the first problem encountered
Example
let templ_str = "Something {data1} be {data2}, and { not here }";
let templ = Template::new(templ_str);
let data = {
let mut map = HashMap::new();
map.insert("data1".to_string(), "should");
map.insert("data2".to_string(), "here");
map
};
let rendered = templ.render_string(&data).expect("Expected Result to be Ok");
assert_eq!("Something should be here, and { not here }", rendered);Render the template with the provided values.
This function takes a HashMap where the key is str.
This function always returns a String, this function does not error or panic.
If Template::render returned a Err, this function will instead return the raw Template string.
Example
let templ_str = "Something {data1} be {data2}, and { not here }";
let templ = Template::new(templ_str);
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);Render the template with the provided values.
This function takes a HashMap where the key is String.
This function always returns a String, this function does not error or panic.
If Template::render_string returned a Err, this function will instead return the raw Template string.
Example
let templ_str = "Something {data1} be {data2}, and { not here }";
let templ = Template::new(templ_str);
let data = {
let mut map = HashMap::new();
map.insert("data1".to_string(), "should");
// map.insert("data2", "here");
map
};
let rendered = templ.render_nofail_string(&data);
assert_eq!("Something should be {data2}, and { not here }", rendered);