Skip to main content

Templar

Struct Templar 

Source
pub struct Templar { /* private fields */ }
Expand description

The Templar struct is the primary template parser.

A new customized instance can be created using TemplarBuilder or alternatively Templar::global() can be used if the default configurations are acceptable.

§Usage

use templar::*;

let context = StandardContext::new();

// parse and render a template, render returns a string
let template = Templar::global().parse("This is a template with {{ 'an expression' }}")?;
assert_eq!(template.render(&context)?, "This is a template with an expression");

// parse and execute an expression, this can be converted to most native types
let expression = Templar::global().parse_expression("5 + 5")?;
assert_eq!(*expression.exec(&context), 10 as i64);

Implementations§

Source§

impl Templar

Source

pub fn parse_template(&self, input: &str) -> Result<Template>

Parse a template string into a Template

Source

pub fn parse_expression(&self, input: &str) -> Result<Template>

Parses an expression string into a Template

Source§

impl Templar

Source

pub fn global() -> &'static Templar

Retrieve the global default instance of Templar when the defaults meet your needs.

Source

pub fn parse<T: Parseable<U>, U>(&self, data: T) -> Result<U>

Parse a Template or TemplateTree value.



let template: Template = Templar::global().parse("{{ [5, 8, 3] | index(0) }}")?;
assert_eq!(*template.exec(&context), 5 as i64);
Source

pub fn parse_json(&self, json: &str) -> Result<TemplateTree>

Parse a JSON string to a TemplateTree. This is useful if you want to parse a configuration file directly to a context as TemplateTree is directly convertible to a context.

§Usage

let json_string = r#"
{
    "key": "{{ script('echo -n test') | key('stdout') }}"
}
"#;


let tree = Templar::global().parse_json(json_string)?;
let template: Template = tree.get_path(&["key"]).try_into()?;

assert_eq!(template.render(&context)?, "test");
Source

pub fn parse_yaml(&self, yml: &str) -> Result<TemplateTree>

Identical to parse_json except this expects a YAML string.

Trait Implementations§

Source§

impl Default for Templar

Source§

fn default() -> Templar

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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