Struct new_string_template::template::Template
source · 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);
Trait Implementations§
impl StructuralPartialEq for Template
Auto Trait Implementations§
impl Freeze for Template
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnwindSafe for Template
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)