Crate map_to_javascript_html[−][src]
Map to JavaScript in HTML
This is a library for serializing a (hash) map to JavaScript code in HTML, usually for dynamically generating strings on web pages.
Example
In your HTML or templates to generate HTML, such as Handlebars, for instance,
<script>
var _text = {};
{{{text}}}
</script>
Then, you can use this crate to insert your text into JavaScript code in HTML,
extern crate map_to_javascript_html; use map_to_javascript_html::hash_map_to_javascript_html; use std::collections::HashMap; let mut map = HashMap::new(); map.insert("hello", "Hello world!"); map.insert("welcome", "Welcome to my website."); map.insert("other keys", "Hello world!"); let text = hash_map_to_javascript_html(&map, "_text", &["welcome", "hello"]).unwrap();
If you want your text to be beautified, you can use hash_map_to_javascript_html_beautify
function.
After Handlebars replaces {{{text}}} with your text, the HTML will be,
<script>
var _text = {};
_text['welcome']='Welcome to my website.';_text['hello']='Hello world!';
</script>
The key and the value used in a map must implement the Display
trait.
Functions
hash_map_to_javascript_html |
Convert a HashMap to minified JavaScript code in HTML. |
hash_map_to_javascript_html_beautify |
Convert a HashMap to beautified JavaScript code in HTML. |