map_to_javascript_html/lib.rs
1/*!
2# Map to JavaScript in HTML
3
4This is a library for serializing a map to JavaScript code in HTML, usually for dynamically generating strings on web pages.
5
6## Usage
7
8In your HTML or templates to generate HTML, such as Handlebars, for instance,
9
10```html
11<script>
12var _text = {};
13{{{text}}}
14</script>
15```
16
17Then, you can use the `MapToJavaScriptHTML` trait to insert your text from a map,
18
19```rust
20use std::collections::BTreeMap;
21
22use map_to_javascript_html::MapToJavaScriptHTML;
23
24let mut map = BTreeMap::new();
25
26map.insert("hello", "Hello world!");
27map.insert("welcome", "Welcome to my website.");
28map.insert("other keys", "Hello world!");
29
30let text = map.to_javascript_html("_text");
31
32assert_eq!("_text['hello']='Hello world!';_text['other keys']='Hello world!';_text['welcome']='Welcome to my website.';", text);
33```
34
35After Handlebars replaces **{{{text}}}** with your text, the HTML will be,
36
37```html
38<script>
39var _text = {};
40_text['hello']='Hello world!';_text['other keys']='Hello world!';_text['welcome']='Welcome to my website.';
41</script>
42```
43
44The key and the value used in a map must implement the `Display` trait.
45
46Methods suffixed with `_to_string`, `_to_vec`, `_to_writer` can be used to generate HTML.
47
48There are also methods prefixed with `to_javascript_html_with_keys` which can be used with keys to filter the output.
49
50```rust
51use std::collections::BTreeMap;
52
53use map_to_javascript_html::MapToJavaScriptHTML;
54
55let mut map = BTreeMap::new();
56
57map.insert("hello", "Hello world!");
58map.insert("welcome", "Welcome to my website.");
59map.insert("other keys", "Hello world!");
60
61let text = map.to_javascript_html_with_keys("_text", &["hello", "welcome"]);
62
63assert_eq!("_text['hello']='Hello world!';_text['welcome']='Welcome to my website.';", text);
64```
65
66## No Std
67
68Disable the default features to compile this crate without std.
69
70```toml
71[dependencies.map-to-javascript-html]
72version = "*"
73default-features = false
74```
75
76## Serde Support
77
78To support the maps from the `serde` framework, enable the `serde` feature.
79
80```toml
81[dependencies.map-to-javascript-html]
82version = "*"
83features = ["serde"]
84```
85*/
86
87#![cfg_attr(not(feature = "std"), no_std)]
88
89#[macro_use]
90extern crate alloc;
91
92mod maps;
93mod traits;
94
95pub use maps::*;
96pub use traits::*;