Struct new_string_template::template::Template

source ·
pub struct Template { /* private fields */ }
Expand description

Struct to store the template

Implementations§

source§

impl Template

source

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);
source

pub fn with_regex(self, regex: &Regex) -> Self

Change the Regex that is used to resolve the matches from the template string.
The Regex requires to have at least one capture group.

§Example
let templ = Template::new(template_string).with_regex(&custom_regex);
source

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);
source

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);
source

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);
source

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);
source

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§

source§

impl Clone for Template

source§

fn clone(&self) -> Template

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Template

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for Template

source§

fn eq(&self, other: &Template) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for Template

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.