/dna.js

DOM Templating Engine for jQuery

Primary LanguageJavaScript

dna.js Template Cloner

DOM Templating Engine for jQuery

dna.js is the world's easiest to use DOM templating engine.

Bookstore Example

Designate DNA templates with the dna-template class, and put the templates directly into the HTML of your web page.  Use the element's id to indicate the name of the template.  Enclose data fields in DNA strands (double tildes).

HTML for book template
<div>
   <h2>Featured Books</h2>
   <div id=book class=dna-template>
      <div>Title:  <span>~~title~~</span></div>
      <div>Author: <span>~~author~~</span></div>
   </div>
</div>

Then call the dna.clone() function to insert a copy of the template into the DOM.  The supplied JSON data object will be used to populate the fields of the template.

JavaScript call to add book node

dna.clone('book', { title: 'The DOM', author: 'Jan' });

The new element is a DNA clone, and it is appended to the template's parent element (the original template is detached from the DOM and kept for additional cloning).

Resulting HTML with DNA clone

<div>
   <h2>Featured Books</h2>
   <div id=book class=dna-clone>
      <div>Title:  <span>The DOM</span></div>
      <div>Author: <span>Jan</span></div>
   </div>
</div>

Clones can alternatively be generated by providing a URL to a REST resource.

JavaScript call to load data from a REST call

dna.load('book', 'http://dnajs.org/rest/book/3');

Additional Information