Fill in DOM nodes with JSON. Supports arrays and attributes.
<div class="note">
<p class="title">title</p>
<ul class="tags">
<li class="tag">
<span class="name">tag</span>
</li>
</ul>
</div>
var json = {
title : 'Javascript',
tags : [
{ name : 'programming' },
{ name : 'javascript' }
]
};
var render = require('json-to-dom');
render(document.querySelector('.note'), json);
outputs:
<div class="note">
<p class="title">Javascript</p>
<ul class="tags">
<li class="tag">
<span class="name">programming</span>
</li>
<li class="tag">
<span class="name">javascript</span>
</li>
</ul>
</div>
$ component install matthewmueller/json-to-dom
It's simpler and more flexible than most templating / binding engines.
Fills in el
with the template
. template
may either be an object or array.
el = render(document.querySelector('.contact'), author);
If the element is already on the page and you don't want to alter it immediately, use cloneNode(true)
var body = render(document.body.cloneNode(true), json);
// stuff
document.body.innerHTML = body.innerHTML;
<ul class="people">
<li class="person">
<span class="first">First Name<span>
<span class="last">Last Name</span>
</li>
</ul>
var people = [
{ first : "matt", last : "mueller" },
{ first : "drew", last : "quinn" }
];
render(document.querySelector('.people'), people);
outputs:
<ul class="people">
<li class="person">
<span class="first">matt<span>
<span class="last">mueller</span>
</li>
<li class="person">
<span class="first">drew<span>
<span class="last">quinn</span>
</li>
</ul>
<ul class="people">
<li class="person">
<span class="key">name</span>
</li>
</ul>
var people = ['matt', 'drew'];
render(document.querySelector('.people'), people);
outputs:
<ul class="people">
<li class="person">
<span class="key">matt<span>
</li>
<li class="person">
<span class="key">drew<span>
</li>
</ul>
<div class="email">
<div class="subject">subject</div>
<div class="from">from</div>
<div class="to">to</div>
<div class="message">message</div>
</div>
var email = {
subject : 'You inherited $11.3 million from the death of your uncle',
from : '[email protected]',
to : '[email protected]',
message : 'Reply with your bank credentials so we can send you the money'
}
render(document.querySelector('.email'), email)
outputs:
<div class="email">
<div class="subject">You inherited $11.3 million from the death of your uncle</div>
<div class="from">[email protected]</div>
<div class="to">[email protected]</div>
<div class="message">Reply with your bank credentials so we can send you the money</div>
</div>
data-html
: will set theinnerHTML
data-text
: will set theinnerText
(the default)data-[attr]
: will set theattr
(ex.data-value
)
When you use data attributes, you have access to the other keys in the object block.
<div class="note">
<a class="title" data-href="url"></a>
</div>
var note = {
title : "This is a note",
url : "http://notes.com"
}
render(document.querySelector('.note'), note);
outputs:
<div class="note">
<a class="title" href="http://notes.com">This is a note</a>
</div>
<div class="note">
<a class="title" data-href="title"></a>
</div>
outputs:
<div class="note">
<a class="title" href="this is a note"></a>
</div>
MIT