pub struct Template { /* private fields */ }
Expand description
Struct to store the template
Implementations§
Source§impl Template
impl Template
Sourcepub fn new<T: Into<String>>(template: T) -> Self
pub fn new<T: Into<String>>(template: T) -> Self
Create a new Template Instance with the default regex.
§Example
let input_template = "Some { Template }";
let template_instance = Template::new(input_template);
Sourcepub fn with_regex(self, regex: &Regex) -> Self
pub fn with_regex(self, regex: &Regex) -> Self
Sourcepub fn new_regex<T: Into<String>>(template: T, regex: &Regex) -> Self
pub fn new_regex<T: Into<String>>(template: T, regex: &Regex) -> Self
Create a new Template Instance with a custom regex
§Example
let templ = Template::new_regex(template_string, &custom_regex);
Sourcepub fn render<T: AsRef<str>>(
&self,
values: &HashMap<&str, T>,
) -> Result<String, TemplateError>
pub fn render<T: AsRef<str>>( &self, values: &HashMap<&str, T>, ) -> Result<String, TemplateError>
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);
Sourcepub 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);
Sourcepub fn render_nofail<T: AsRef<str>>(&self, values: &HashMap<&str, T>) -> String
pub fn render_nofail<T: AsRef<str>>(&self, values: &HashMap<&str, T>) -> String
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);
Sourcepub fn render_nofail_string<T: AsRef<str>>(
&self,
values: &HashMap<String, T>,
) -> String
pub fn render_nofail_string<T: AsRef<str>>( &self, values: &HashMap<String, T>, ) -> String
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);