Expand description
§A minimal text template engine
§Overview
Create templates with named placeholders within it.
Placeholders are defined by default following the handlebars syntax, but can be overriden with specific boundaries.
This library supports passing a HashMap
or Struct
as a context
in order to replace the specified placeholders.
§Example
use text_placeholder::Template;
#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
let default_template = Template::new("Hello {{first}} {{second}}!");
let mut table = HashMap::new();
table.insert("first", "text");
table.insert("second", "placeholder");
assert_eq!(default_template.fill_with_hashmap(&table), "Hello text placeholder!");
// We can also specify our own boundaries:
let custom_template = Template::new_with_placeholder("Hello $[first]] $[second]!", "$[", "]");
assert_eq!(default_template.fill_with_hashmap(&table), "Hello text placeholder!");
Structs§
- Template
- A template is composed of tokens, which in turn can represent plain text or a named placeholder.