| Crates.io | simple-html-template |
| lib.rs | simple-html-template |
| version | 0.2.4 |
| created_at | 2020-07-14 13:26:10.503178+00 |
| updated_at | 2020-11-16 16:03:53.75868+00 |
| description | Html Template |
| homepage | https://github.com/dakom/simple-html-template |
| repository | https://github.com/dakom/simple-html-template |
| max_upload_size | |
| id | 265057 |
| size | 37,262 |
This is essentially a fork of far, with some additions:
The html_map! and html_map_strong! macros use the htmlescape crate, which you must add as a dependency (or else compilation will fail when calling these macros)
Note that, like far, this crate does not deal with escaping the keys or replacements in any way. e.g. if for some reason you need the template to have a ${} literal.
The value of the HashMap which is passed to Template::render() must be AsRef<str>
Examples:
Provided with a string and a map, simple-html-template will attempt to find
all the keys (delimited with ${}) in the template and replace them with
the corresponding value in the map. For example:
let template = Template::new("${capitalized specific} are my favorite ${category}.")?;
let args = html_map!{
"capitalized specific" => "Cats",
"category" => "animal",
};
let s = template.render(&args)?;
assert_eq!(s, "Cats are my favorite animal.");
If it fails for some reason, an explanation of why will be returned:
let template = Template::new("${capitalized specific} are my favorite ${category}.")?;
let args = html_map!{
"capitalized specific" => "Cats",
// Note the typo here
"catglory" => "animal",
};
match template.render(&args) {
Ok(_) => panic!(),
Err(e) => {
assert_eq!(
format!("{}", e),
r#"missing key: "category"; extraneous key: "catglory""#
);
}
}
Note that if html is in the variable, it is escaped:
let template = Template::new("${capitalized specific} are my favorite ${category}.")?;
let args = html_map!{
"capitalized specific" => "<b>Cats</b>",
"category" => "<i>animal</i>",
};
let s = template.render(&args)?;
assert_eq!(s, "<b>Cats</b> are my favorite <i>animal</i>.");
Additional examples and weird edge-case behaviors can be found in
src/tests.
This project is licensed under either of
Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.