[][src]Struct templar::Templar

pub struct Templar { /* fields omitted */ }

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

impl Templar[src]

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

Parse a template string into a Template

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

Parses an expression string into a Template

impl Templar[src]

pub fn global() -> &'static Templar[src]

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

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

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);

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

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");

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

Identical to parse_json except this expects a YAML string.

Trait Implementations

impl Default for Templar[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.