Crate tinytemplate[][src]

TinyTemplate

TinyTemplate is a minimal templating library originally designed for use in Criterion.rs. It deliberately does not provide all of the features of a full-power template engine, but in return it provides a simple API, clear templating syntax, decent performance and very few dependencies.

Features

The most important features are as follows (see the syntax module for full details on the template syntax):

  • Rendering values - { myvalue }
  • Conditionals - {{ if foo }}Foo is true{{ else }}Foo is false{{ endif }}
  • Loops - {{ for value in row }}{value}{{ endfor }}
  • Customizable value formatters { value | my_formatter }
  • Macros {{ call my_template with foo }}

Restrictions

TinyTemplate was designed with the assumption that the templates are available as static strings, either using string literals or the include_str! macro. Thus, it borrows &str slices from the template text itself and uses them during the rendering process. Although it is possible to use TinyTemplate with template strings loaded at runtime, this is not recommended.

Additionally, TinyTemplate can only render templates into Strings. If you need to render a template directly to a socket or file, TinyTemplate may not be right for you.

Example

#[macro_use]
extern crate serde_derive;
extern crate tinytemplate;

use tinytemplate::TinyTemplate;
use std::error::Error;

#[derive(Serialize)]
struct Context {
    name: String,
}

static TEMPLATE : &'static str = "Hello {name}!";

pub fn main() -> Result<(), Box<Error>> {
    let mut tt = TinyTemplate::new();
    tt.add_template("hello", TEMPLATE)?;

    let context = Context {
        name: "World".to_string(),
    };

    let rendered = tt.render("hello", &context)?;
    println!("{}", rendered);

    Ok(())
}

Modules

error

Module containing the error type returned by TinyTemplate if an error occurs.

syntax

Documentation of TinyTemplate’s template syntax.

Structs

TinyTemplate

The TinyTemplate struct is the entry point for the TinyTemplate library. It contains the template and formatter registries and provides functions to render templates as well as to register templates and formatters.

Functions

escape

Appends value to output, performing HTML-escaping in the process.

format

The format function is used as the default value formatter for all values unless the user specifies another. It is provided publicly so that it can be called as part of custom formatters. Values are formatted as follows:

format_unescaped

Identical to format except that this does not perform HTML escaping.

Type Definitions

ValueFormatter

Type alias for closures which can be used as value formatters.