go_template/
lib.rs

1//! The Golang Templating Language for Rust.
2//!
3//! ## Example
4//! ```rust
5//! use gtmpl;
6//!
7//! let output = gtmpl::template("Finally! Some {{ . }} for Rust", "gtmpl");
8//! assert_eq!(&output.unwrap(), "Finally! Some gtmpl for Rust");
9//! ```
10pub mod error;
11mod exec;
12pub mod funcs;
13mod lexer;
14mod node;
15mod parse;
16mod print_verb;
17mod printf;
18mod template;
19mod utils;
20
21#[doc(inline)]
22pub use crate::template::Template;
23
24#[doc(inline)]
25pub use crate::exec::Context;
26
27#[doc(inline)]
28pub use gtmpl_value::Func;
29
30pub use gtmpl_value::FuncError;
31
32#[doc(inline)]
33pub use gtmpl_value::from_value;
34
35pub use error::TemplateError;
36pub use gtmpl_value::Value;
37
38/// Provides simple basic templating given just a template sting and context.
39///
40/// ## Example
41/// ```rust
42/// let output = gtmpl::template("Finally! Some {{ . }} for Rust", "gtmpl");
43/// assert_eq!(&output.unwrap(), "Finally! Some gtmpl for Rust");
44/// ```
45pub fn template<T: Into<Value>>(template_str: &str, context: T) -> Result<String, TemplateError> {
46    let mut tmpl = Template::default();
47    tmpl.parse(template_str)?;
48    tmpl.render(&Context::from(context)).map_err(Into::into)
49}