Expand description
§Terarium
Terarium is library for rendering groups of templates using the Tera templating library.
§Installation
cargo install terarium
§Usage
To create Terarium
instance, use the TerariumBuilder
. This builder is able to configure templates and groups and
when you add all items, call the build()
method to retrieve the Terarium
instance. When preparing Template
instances, you can add more than one content. Each content is bound to one language key. But language key can have
assigned more than one content.
When instance is ready, call render_template
or render_group
to render single template or template group defined by
its key. Because the library have multi-language support, the language key has to be passed and optional fallback
language key. The fallback language is used when the primary language version of a template is not found.
Render result of the single template render is Result<String, TerariumError>
where the String
is the rendered
content.
Render result of the template group render is Result<HashMap<String, String>, TerariumError>
Where the HashMap
contains the data. Keys of the hashmap is group member keys and values are their rendered contents.
§Example
use tera::Context;
use terarium::{Content, Template, TemplateGroupBuilder, TerariumBuilder};
/// The Terarium can create logical template groups and render them together,
fn main() {
let mut builder = TerariumBuilder::default();
builder.add_template(
"greet_subject".to_owned(),
Template::new(vec![
Content::new("Greetings from {{sender}}".to_owned(), vec!["en".to_owned()]),
Content::new("Pozdrav od {{sender}}".to_owned(), vec!["cs".to_owned()]),
]).unwrap(),
).unwrap();
builder.add_template(
"greet_text".to_owned(),
Template::new(vec![
Content::new("Hello {{username}}".to_owned(), vec!["en".to_owned()]),
Content::new("Nazdar {{username}}".to_owned(), vec!["cs".to_owned()]),
]).unwrap(),
).unwrap();
builder.add_template(
"greet_html".to_owned(),
Template::new(vec![
Content::new("<p>Hello {{username}}</p>".to_owned(), vec!["en".to_owned()]),
Content::new("<p>Nazdar {{username}}</p>".to_owned(), vec!["cs".to_owned()]),
]).unwrap()
).unwrap();
builder.add_group(
"greet_email".to_string(),
TemplateGroupBuilder::default()
.add_member("subject".to_owned(), "greet_subject".to_owned())
.add_member("text".to_owned(), "greet_text".to_owned())
.add_member("html".to_owned(), "greet_html".to_owned())
.build(),
).unwrap();
let terarium = builder.build().unwrap();
let mut ctx = Context::new();
ctx.insert("sender", "Jara Cimrman");
ctx.insert("username", "Karel Capek");
let rendered_group_en = terarium.render_group(&ctx, "greet_email", "en", None).unwrap();
let rendered_group_cs = terarium.render_group(&ctx, "greet_email", "cs", None).unwrap();
println!("\nEnglish");
println!("=======\n");
rendered_group_en.iter().for_each(|(member_key, content)| println!("{}: {}", member_key, content));
println!("\nCzech");
println!("=====\n");
rendered_group_cs.iter().for_each(|(member_key, content)| println!("{}: {}", member_key, content));
}
§Output
English
=======
text: Hello Karel Capek
subject: Greetings from Jara Cimrman
html: <p>Hello Karel Capek</p>
Czech
=====
html: <p>Nazdar Karel Capek</p>
subject: Pozdrav od Jara Cimrman
text: Nazdar Karel Capek
See more examples in the project’s repository.
Re-exports§
pub use tera;
Structs§
- Content
- Represent content of template
- Template
- Contains data for
Tera
template with language mutations. - Template
Group Builder - Simplify building template groups.
- Terarium
- Wrapper over the
Tera
templating engine with capability of template bulk rendering. Each template can exists in more than one version (support for multi-language templates). An instance of theTerarium
is built with theTerariumBuilder
. - Terarium
Builder - Build the
Terarium
instance.
Enums§
- Template
Error - Errors returned by template operations.
- Terarium
Builder Error - Errors returned by
TerariumBuilder
struct. - Terarium
Error - Errors returned by
Terarium
operations.