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

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

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.