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
impl Templar
Sourcepub fn parse_template(&self, input: &str) -> Result<Template>
pub fn parse_template(&self, input: &str) -> Result<Template>
Parse a template string into a Template
Sourcepub fn parse_expression(&self, input: &str) -> Result<Template>
pub fn parse_expression(&self, input: &str) -> Result<Template>
Parses an expression string into a Template
Source§impl Templar
impl Templar
Sourcepub fn global() -> &'static Templar
pub fn global() -> &'static Templar
Retrieve the global default instance of Templar when the defaults meet your needs.
Sourcepub fn parse<T: Parseable<U>, U>(&self, data: T) -> Result<U>
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);Sourcepub fn parse_json(&self, json: &str) -> Result<TemplateTree>
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");Sourcepub fn parse_yaml(&self, yml: &str) -> Result<TemplateTree>
pub fn parse_yaml(&self, yml: &str) -> Result<TemplateTree>
Identical to parse_json except this expects a YAML string.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Templar
impl !RefUnwindSafe for Templar
impl Send for Templar
impl Sync for Templar
impl Unpin for Templar
impl UnsafeUnpin for Templar
impl !UnwindSafe for Templar
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