#map #javascript #html #hashmap #btreemap

no-std map-to-javascript-html

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

12 stable releases

2.0.4 Apr 21, 2021
2.0.3 Jul 29, 2020
2.0.1 Jun 28, 2020
1.2.3 Sep 17, 2019
0.1.1 Aug 22, 2018

#226 in Encoding

Download history 15/week @ 2021-02-20 4/week @ 2021-02-27 4/week @ 2021-03-06 1/week @ 2021-03-13 42/week @ 2021-03-20 31/week @ 2021-03-27 30/week @ 2021-04-03 19/week @ 2021-04-10 17/week @ 2021-04-17 19/week @ 2021-04-24 20/week @ 2021-05-01 16/week @ 2021-05-15 17/week @ 2021-06-05

95 downloads per month

MIT license

25KB
478 lines

Map to JavaScript in HTML

CI

This is a library for serializing a map to JavaScript code in HTML, usually for dynamically generating strings on web pages.

Usage

In your HTML or templates to generate HTML, such as Handlebars, for instance,

<script>
var _text = {};
{{{text}}}
</script>

Then, you can use the MapToJavaScriptHTML trait to insert your text from a map,

extern crate map_to_javascript_html;

use std::collections::BTreeMap;

use map_to_javascript_html::MapToJavaScriptHTML;

let mut map = BTreeMap::new();

map.insert("hello", "Hello world!");
map.insert("welcome", "Welcome to my website.");
map.insert("other keys", "Hello world!");

let text = map.to_javascript_html("_text");

assert_eq!("_text['hello']='Hello world!';_text['other keys']='Hello world!';_text['welcome']='Welcome to my website.';", text);

After Handlebars replaces {{{text}}} with your text, the HTML will be,

<script>
var _text = {};
_text['hello']='Hello world!';_text['other keys']='Hello world!';_text['welcome']='Welcome to my website.';
</script>

The key and the value used in a map must implement the Display trait.

Methods suffixed with _to_string, _to_vec, _to_writer can be used to generate HTML.

There are also methods prefixed with to_javascript_html_with_keys which can be used with keys to filter the output.

extern crate map_to_javascript_html;

use std::collections::BTreeMap;

use map_to_javascript_html::MapToJavaScriptHTML;

let mut map = BTreeMap::new();

map.insert("hello", "Hello world!");
map.insert("welcome", "Welcome to my website.");
map.insert("other keys", "Hello world!");

let text = map.to_javascript_html_with_keys("_text", &["hello", "welcome"]);

assert_eq!("_text['hello']='Hello world!';_text['welcome']='Welcome to my website.';", text);

No Std

Disable the default features to compile this crate without std.

[dependencies.map-to-javascript-html]
version = "*"
default-features = false

Serde Support

To support the maps from the serde framework, enable the serde feature.

[dependencies.map-to-javascript-html]
version = "*"
features = ["serde"]

Crates.io

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

Documentation

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

License

MIT

Dependencies

~165–310KB