Struct srtemplate::SrTemplate
source · 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 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 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
Examples found in repository?
More examples
sourcepub fn add_function<T: Into<Cow<'a, str>>>(
&self,
name: T,
func: TemplateFunction
)
pub fn add_function<T: Into<Cow<'a, str>>>( &self, name: T, func: TemplateFunction )
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
Examples found in repository?
More examples
sourcepub fn render(&self, text: &str) -> Result<String, SrTemplateError>
pub fn render(&self, text: &str) -> Result<String, SrTemplateError>
Renders text as a template, replacing variables and processing functions.
Arguments
text: The text in template format to be processed.
Returns
A Result where Ok contains the rendered template as a String, and Err holds a SrTemplateError if an error occurs.
Example
use srtemplate::prelude::SrTemplate;
use srtemplate::prelude::SrTemplateError;
let ctx = SrTemplate::default();
let template = "Hello, {{ name }}!";
match ctx.render(template) {
Ok(rendered) => println!("Rendered: {}", rendered),
Err(err) => eprintln!("Error: {:?}", err),
}Examples found in repository?
More examples
examples/handle_errors.rs (line 8)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() {
let ctx = SrTemplate::default();
let template = "Hi {{ toTitle(var) }}";
match ctx.render(template) {
Ok(result) => println!("Rendered: {result}"),
Err(e) => match e {
srtemplate::SrTemplateError::BadSyntax(e) => println!("Invalid syntaxis: {e}"),
srtemplate::SrTemplateError::VariableNotFound(e) => println!("Variable not found: {e}"),
srtemplate::SrTemplateError::FunctionNotImplemented(e) => {
println!("Function not supported: {e}")
}
srtemplate::SrTemplateError::Function(e) => println!("Error procesing function: {e}"),
},
}
}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>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<'a> !RefUnwindSafe for SrTemplate<'a>
impl<'a> Send for SrTemplate<'a>
impl<'a> Sync for SrTemplate<'a>
impl<'a> Unpin for SrTemplate<'a>
impl<'a> !UnwindSafe for SrTemplate<'a>
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
Mutably borrows from an owned value. Read more