terarium 0.2.0

Extension of the Tera template system with template grouping and bulk rendering of templates.
Documentation

Terarium

Tests

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

fn main() {
    let terarium = TerariumBuilder::default()
        .add_template(
            "greet_subject".to_owned(),
            Template::default()
                .add_content(Content::new("Greetings from {{sender}}".to_owned(), vec!["en".to_owned()])).unwrap()
                .add_content(Content::new("Pozdrav od {{sender}}".to_owned(), vec!["cs".to_owned()])).unwrap()
        ).unwrap()
        .add_template(
            "greet_text".to_owned(),
            Template::default()
                .add_content(Content::new("Hello {{username}}".to_owned(), vec!["en".to_owned()])).unwrap()
                .add_content(Content::new("Nazdar {{username}}".to_owned(), vec!["cs".to_owned()])).unwrap()
        ).unwrap()
        .add_template(
            "greet_html".to_owned(),
            Template::default()
                .add_content(Content::new("<p>Hello {{username}}</p>".to_owned(), vec!["en".to_owned()])).unwrap()
                .add_content(Content::new("<p>Nazdar {{username}}</p>".to_owned(), vec!["cs".to_owned()])).unwrap()
        ).unwrap()
        .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()
        .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.

Note

There is no typo in name of this library. Double r could lead to confusion with the Terra library which is absolutely out of this library scope. :-)

Changes in 0.2

  • Simplification of the Template struct api (when content is added, it cannot be changed).
  • Template.add_content() now return Result<Self, TemplateError>.
  • Content can be named (and thus it can be referenced in other templates).
  • ContentBuilder struct removed.
  • The language assignment to Content must be unique in one Template.
  • Template, language and other keys are not generic anymore. All keys are String now.
  • Attempt to define invalid template group now return Result::Err and Terarium::check_group_config_validity is removed from interface.
  • TerariumBuilder::add_template() now return Result<Self, TerariumBuilderError> to get symmetry with the TerariumBuilder::add_group() method.

For full changelog see CHANGELOG.md