#hashmap #javascript #html #web #serialization

map-to-javascript-html

A library for serializing a (hash) map to JavaScript code in HTML usually for dynamically generating strings on web pages

9 releases (stable)

1.2.1 Nov 14, 2018
1.2.0 Oct 15, 2018
1.1.2 Sep 7, 2018
0.1.1 Aug 22, 2018

#83 in Parsing tools

Download history 107/week @ 2018-10-16 4/week @ 2018-10-23 55/week @ 2018-10-30 54/week @ 2018-11-06 36/week @ 2018-11-13 19/week @ 2018-11-20 74/week @ 2018-11-27 41/week @ 2018-12-04 19/week @ 2018-12-11 42/week @ 2018-12-18 30/week @ 2018-12-25 10/week @ 2019-01-01 20/week @ 2019-01-08 5/week @ 2019-01-15 6/week @ 2019-01-22

132 downloads per month

MIT license

9KB
124 lines

Map to JavaScript in HTML

Build Status Build status

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.

By the way, the text_to_javascript_html function can be useful when you just want to insert text as a JavaScript string into your HTML code.

Crates.io

https://crates.io/crates/map-to-javascript-html

Documentation

https://docs.rs/map-to-javascript-html

License

MIT

Dependencies

~1.5MB
~32K SLoC