SrTemplate

Struct 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>

Source

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 a Cow<str>.
  • close: The end delimiter. This is a string slice or a type that can be converted into a Cow<str>.
Source

pub fn add<V: Variable<'a>>(&self, value: V)

Source

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 template
  • value: This is the value on which the template will be replaced in the template
Source

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 template
  • value: This is the value on which the template will be replaced in the template
Source

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 template
  • func: This is the function that will be evaluated when it is called from the template
Source

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 template
  • func: This is the function that will be evaluated when it is called from the template
Source

pub fn contains_variable<T: Into<Cow<'a, str>>>(&self, name: T) -> bool

Checks if a variable exists in the template string by its name.

§Arguments
  • name - The name of the variable to check.
§Returns
  • bool - true if the variable exists, false otherwise.
Source

pub fn contains_function<T: Into<Cow<'a, str>>>(&self, name: T) -> bool

Checks if a function exists in the template string by its name.

§Arguments
  • name - The name of the function to check.
§Returns
  • bool - true if the function exists, false otherwise.
Source

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.
Source

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.
Source

pub fn clear_variables(&self)

Clears all variables from the template string.

Source

pub fn clear_functions(&self)

Clears all functions from the template string.

Source

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 a Cow<str>.
  • close: The end delimiter. This is a string slice or a type that can be converted into a Cow<str>.
Source

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>

Source§

fn clone(&self) -> SrTemplate<'a>

Returns a duplicate 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 Default for SrTemplate<'_>

Source§

fn default() -> Self

Generates an instance with all the builtin functions that are enabled from features

Auto Trait Implementations§

§

impl<'a> Freeze for SrTemplate<'a>

§

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> 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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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>,

Source§

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>,

Source§

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.