Text Placeholder
A flexible text template engine that allows templates with named placeholders within it.
let template = new;
Placeholders are defined by default following the handlebars syntax, but can be overriden with specific boundaries.
let template = new_with_placeholder;
In order to provide the context in which our placeholders will be replaced the following options are available:
- HashMap.
- Struct, as an optional feature.
HashMap
The following methods are available with a HashMap
:
fill_with_hashmap
- replaces missing placeholders with an empty string.
- replaces placeholders that cannot be converted to a strint with an empty string.
fill_with_hashmap_strict
which returns aError::PlaceholderError
when:- a placeholder is missing.
- a placeholder value cannot be converted to a string.
Example
use Template;
use HashMap;
let default_template = new;
let mut table = new;
table.insert;
table.insert;
assert_eq!;
// We can also specify our own boundaries:
let custom_template = new_with_placeholder;
assert_eq!;
Struct
Allow structs that implement the serde::Serialize
trait to be used as context.
This is an optional feature that depends on serde
. In order to enable it add the following to your Cargo.toml
file:
[]
= { = "0.3", = ["struct_context"] }
The methods available are:
fill_with_struct
- replaces missing placeholders with an empty string.
- replaces placeholders that cannot be converted to a strint with an empty string.
fill_with_struct_strict
which returns aError::PlaceholderError
when:- a placeholder is missing.
- a placeholder value cannot be converted to a string.
Example
use Template;
let default_template = new;
let context = Context ;
assert_eq!;
// We can also specify our own boundaries:
let custom_template = new_with_placeholder;
assert_eq!;