Giter Club home page Giter Club logo

map-to-javascript-html's Introduction

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

map-to-javascript-html's People

Contributors

magiclen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.