Crates.io | map-to-javascript-html |
lib.rs | map-to-javascript-html |
version | 2.0.5 |
source | src |
created_at | 2018-08-22 03:25:55.441817 |
updated_at | 2022-03-17 13:46:37.778276 |
description | A library for serializing a map to JavaScript code in HTML usually for dynamically generating strings on web pages. |
homepage | https://magiclen.org/map-to-javascript-html |
repository | https://github.com/magiclen/map-to-javascript-html |
max_upload_size | |
id | 80677 |
size | 25,969 |
This is a library for serializing a map to JavaScript code in HTML, usually for dynamically generating strings on web pages.
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,
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.
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);
Disable the default features to compile this crate without std.
[dependencies.map-to-javascript-html]
version = "*"
default-features = false
To support the maps from the serde
framework, enable the serde
feature.
[dependencies.map-to-javascript-html]
version = "*"
features = ["serde"]
https://crates.io/crates/map-to-javascript-html
https://docs.rs/map-to-javascript-html