pub struct SrTemplate<'a> { /* private fields */ }Expand description
Re-exports the template::function, template::SrTemplate, [template::TemplateFunction] type for convenient use.
This structure is the basis of everything, it is responsible for managing variables and functions.
§Examples
use srtemplate::SrTemplate;
let mut ctx = SrTemplate::default();
ctx.add_variable("var", &"World");
ctx.add_variable("otherVar", &"Other");
ctx.add_variable("number", &85u8);
ctx.render("Hello {{ var }}! This is {{ otherVar }} and this is number: {{number}}").unwrap();Implementations§
Source§impl<'a> SrTemplate<'a>
impl<'a> SrTemplate<'a>
Sourcepub fn with_delimiter<U: Into<Cow<'a, str>>>(start: U, close: U) -> Self
pub fn with_delimiter<U: Into<Cow<'a, str>>>(start: U, close: U) -> Self
Create instance of SrTemplate with custom delimiters
§Arguments
start: The start delimiter. This is a string slice or a type that can be converted into aCow<str>.close: The end delimiter. This is a string slice or a type that can be converted into aCow<str>.
pub fn add<V: Variable<'a>>(&self, value: V)
Sourcepub fn add_variable<U: Into<Cow<'a, str>>, T: ToString>(
&self,
name: U,
value: T,
)
pub fn add_variable<U: Into<Cow<'a, str>>, T: ToString>( &self, name: U, value: T, )
Adds variable that can later be rendered in the template
§Arguments
name: Variable name, this name is the one you will use in the templatevalue: This is the value on which the template will be replaced in the template
Sourcepub fn add_variables<U: Into<Cow<'a, str>>, V: Iterator<Item = (U, &'a dyn ToString)>>(
&self,
values: V,
)
pub fn add_variables<U: Into<Cow<'a, str>>, V: Iterator<Item = (U, &'a dyn ToString)>>( &self, values: V, )
Adds variables that can later be rendered in the template
§Arguments
name: Variable name, this name is the one you will use in the templatevalue: This is the value on which the template will be replaced in the template
Sourcepub fn add_function<T: Into<Cow<'a, str>>>(&self, name: T, func: Function)
pub fn add_function<T: Into<Cow<'a, str>>>(&self, name: T, func: Function)
Adds function that can later be rendered in the template
§Arguments
name: Function name, this name is the one you will use in the templatefunc: This is the function that will be evaluated when it is called from the template
Sourcepub fn add_functions<U: Into<Cow<'a, str>>, V: Iterator<Item = (U, Function)>>(
&self,
values: V,
)
pub fn add_functions<U: Into<Cow<'a, str>>, V: Iterator<Item = (U, Function)>>( &self, values: V, )
Adds functions that can later be rendered in the template
§Arguments
name: Function name, this name is the one you will use in the templatefunc: This is the function that will be evaluated when it is called from the template
Sourcepub fn remove_variable<T: Into<Cow<'a, str>>>(&self, name: T)
pub fn remove_variable<T: Into<Cow<'a, str>>>(&self, name: T)
Removes a variable from the template string by its name.
§Arguments
name- The name of the variable to remove.
Sourcepub fn remove_function<T: Into<Cow<'a, str>>>(&self, name: T)
pub fn remove_function<T: Into<Cow<'a, str>>>(&self, name: T)
Removes a function from the template string by its name.
§Arguments
name- The name of the function to remove.
Sourcepub fn clear_variables(&self)
pub fn clear_variables(&self)
Clears all variables from the template string.
Sourcepub fn clear_functions(&self)
pub fn clear_functions(&self)
Clears all functions from the template string.
Sourcepub fn set_delimiter<U: Into<Cow<'a, str>>>(&mut self, start: U, close: U)
pub fn set_delimiter<U: Into<Cow<'a, str>>>(&mut self, start: U, close: U)
Sets the delimiters for the template string.
This function allows you to define the start and end delimiters that will be used to identify the content within the template string. The delimiters can be any string or character sequence that does not conflict with the template content.
§Arguments
start: The start delimiter. This is a string slice or a type that can be converted into aCow<str>.close: The end delimiter. This is a string slice or a type that can be converted into aCow<str>.
Sourcepub fn render<T: AsRef<str>>(&self, text: T) -> Result<String, Error>
pub fn render<T: AsRef<str>>(&self, text: T) -> Result<String, Error>
Renders a template by replacing variables and processing functions.
§Arguments
text- A template string to be rendered.
§Returns
A Result where:
Ok(String)contains the rendered template as a string.Err(Error)contains the details of an error if rendering fails.
§Example
use srtemplate::prelude::SrTemplate;
let ctx = SrTemplate::default();
let template = "Hello, {{ name }}!";
match ctx.render(template) {
Ok(rendered) => println!("Rendered: {}", rendered),
Err(err) => eprintln!("Error: {:?}", err),
}§Errors
Returns an error if:
- The syntax of the template is invalid.
- A variable or function is not found or fails during processing.
Trait Implementations§
Source§impl<'a> Clone for SrTemplate<'a>
impl<'a> Clone for SrTemplate<'a>
Source§fn clone(&self) -> SrTemplate<'a>
fn clone(&self) -> SrTemplate<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more