Crate nanotemplate

Crate nanotemplate 

Source
Expand description

A minimal templating engine that renders a string from the template, replacing all instances of {placeholder} with given values.

The engine is strict:

  • all placeholders must have values provided (use template_default to use default value for placeholders),
  • all provided values must have matching placeholder (when using template_strict),
  • a single placeholder can be used multiple times and will be expanded in all places.

Values are provided as an iterable object that provides placeholder name and value pairs.

Placeholder name may only contain letters (a to z, A to Z), numbers, dash, underscore and a dot to limit the risk of accidental placeholders in templates.

Hash maps are not used; all data is kept in vectors making it O(N + N * M) where N is number of placeholders and M is number of values. This should still be very fast for templates with small number of placeholders as no data is copied (works with slices).

use nanotemplate::template;

assert_eq!(
    template("Hello, my name is {name}!", &[("name", "nanotemplate")]).unwrap(),
    "Hello, my name is nanotemplate!".to_owned());

Also comes with simple CLI utility:

echo "Hello, my name is {name}!" | nanotemplate name=nanotemplate

Enums§

TemplateError

Traits§

PlaceholderValuePair
Provides a way to get placeholder name and value for template expansion.

Functions§

template
Renders a string from the template, replacing all instances of {placeholder} with given values.
template_default
Renders a string from the template, replacing all instances of {placeholder} with given values.
template_strict
Renders a string from the template, replacing all instances of {placeholder} with given values.
write_template
Variant of template() that write outputs to out.
write_template_default
Variant of template_default() that writes output to out.
write_template_strict
Variant of template_strict() that writes output to out.