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>

source

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 template
  • value: This is the value on which the template will be replaced in the template
Examples found in repository?
examples/render_struct.rs (line 20)
15
16
17
18
19
20
21
22
23
24
fn main() {
    let ctx = SrTemplate::default();

    let p = Point { x: 0.0, y: 5.025 };

    ctx.add_variable("point", &p);

    let template = "Point {{ point }}";
    println!("Rendered: {}", ctx.render(template).unwrap());
}
More examples
Hide additional examples
examples/custom_func.rs (line 21)
19
20
21
22
23
24
25
26
27
fn main() {
    let ctx = SrTemplate::default();
    ctx.add_variable("var", &"mUnDo");
    ctx.add_function("toTitle", to_title);

    let template = "Hola {{ toTitle(var) }}";

    println!("Rendered: {}", ctx.render(template).unwrap());
}
examples/parse_args.rs (line 18)
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let ctx = SrTemplate::default();
    ctx.add_variable("var", &"mUnDo");
    ctx.add_variable("other", &255u8);

    ctx.add_function("merge", merge);

    let template = "Hola {{ merge(var, other) }}";

    println!("Rendered: {}", ctx.render(template).unwrap());
}
examples/variables.rs (line 5)
3
4
5
6
7
8
9
10
11
fn main() {
    let ctx = SrTemplate::default();
    ctx.add_variable("var", &"World");
    ctx.add_variable("otherVar", &"Other");
    ctx.add_variable("number", &85u8);

    let template = "Hello {{ var }}! This is {{ otherVar }} and this is number: {{number}}";
    println!("Rendered: {}", ctx.render(template).unwrap());
}
source

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 template
  • func: This is the function that will be evaluated when it is called from the template
Examples found in repository?
examples/custom_func.rs (line 22)
19
20
21
22
23
24
25
26
27
fn main() {
    let ctx = SrTemplate::default();
    ctx.add_variable("var", &"mUnDo");
    ctx.add_function("toTitle", to_title);

    let template = "Hola {{ toTitle(var) }}";

    println!("Rendered: {}", ctx.render(template).unwrap());
}
More examples
Hide additional examples
examples/parse_args.rs (line 21)
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let ctx = SrTemplate::default();
    ctx.add_variable("var", &"mUnDo");
    ctx.add_variable("other", &255u8);

    ctx.add_function("merge", merge);

    let template = "Hola {{ merge(var, other) }}";

    println!("Rendered: {}", ctx.render(template).unwrap());
}
source

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?
examples/render_struct.rs (line 23)
15
16
17
18
19
20
21
22
23
24
fn main() {
    let ctx = SrTemplate::default();

    let p = Point { x: 0.0, y: 5.025 };

    ctx.add_variable("point", &p);

    let template = "Point {{ point }}";
    println!("Rendered: {}", ctx.render(template).unwrap());
}
More examples
Hide additional examples
examples/custom_func.rs (line 26)
19
20
21
22
23
24
25
26
27
fn main() {
    let ctx = SrTemplate::default();
    ctx.add_variable("var", &"mUnDo");
    ctx.add_function("toTitle", to_title);

    let template = "Hola {{ toTitle(var) }}";

    println!("Rendered: {}", ctx.render(template).unwrap());
}
examples/parse_args.rs (line 25)
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let ctx = SrTemplate::default();
    ctx.add_variable("var", &"mUnDo");
    ctx.add_variable("other", &255u8);

    ctx.add_function("merge", merge);

    let template = "Hola {{ merge(var, other) }}";

    println!("Rendered: {}", ctx.render(template).unwrap());
}
examples/variables.rs (line 10)
3
4
5
6
7
8
9
10
11
fn main() {
    let ctx = SrTemplate::default();
    ctx.add_variable("var", &"World");
    ctx.add_variable("otherVar", &"Other");
    ctx.add_variable("number", &85u8);

    let template = "Hello {{ var }}! This is {{ otherVar }} and this is number: {{number}}";
    println!("Rendered: {}", ctx.render(template).unwrap());
}
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>

source§

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

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

source§

fn default() -> Self

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

Auto 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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.