DOMtempl

HTML templating that brings you peace

Documentation - Node.js

Quick start

Node.js version of DOMtempl relies on jsdom (or optionally, some other DOM implementation) to function.

npm install jsdom domtempl

Thus, the initialization steps are a bit different.

 
    var jsdom = require("jsdom").jsdom;
    var DOMtempl = require("domtempl");
 
 

Reading the template

 
    var document = jsdom('<html><p data-var="foo"></p></html>');
    var l = new DOMtempl(document);
 
 

or

 
    var l = new DOMtempl(jsdom('<html><p data-var="foo"></p></html>'));
 
 

Reading variables defined/expected by template

 
    var l = new DOMtempl(jsdom('<html><p data-var="foo">Boo</p></html>'));
 
    console.log(l.vars);
    //{ "foo" : "Boo" }
 
 

Writing variables

 
    l.assign("foo", "Hello world");
 
 

You can also assign variables by accessing the vars public property,

 
    l.vars['foo'] = "Hello world";
 
 

has the same effect.

Writing output

 
    var html = l.dump();
 
    console.log(html);
 
 

Complete example

var jsdom = require("jsdom").jsdom;
var DOMtempl = require("domtempl");
 
var document = jsdom('
<html>
    <head>
        <meta charset="utf-8" />
    </head>
    <body>
        <h1 data-var="title">
            Page Title
        </h1>
    </body>
</html>
');
var t = new DOMtempl(document);
alert(t.vars['title']); // this will output "Page Title"
t.vars['title'] = 'Custom Title';
t.reflow(); //this will change the H1 above to "Custom Title"
 
console.log( t.dump() ); //output new HTML