templet v0.0.3
#Templet
npm install templet
A tiny templating engine for Node.
##Usage
Templet does no caching or inline JavaScript evaluation (yet). Any ulterior responsibilities are left to you. It simply renders some template pseudo-HTML based on a mustache style {{
(open) and }}
(close) tags.
var templet = require('templet');
templet.render('The time is {{time}}', {time:new Date().toUTCString()});
...or you can set the open/close tags yourself and try your luck with your existing templates:
templet.open('<%');
templet.set('close', '%>');
###Using default templates
Templet comes with a base template which has two variables: title
and body
.
var templet = require('templet');
var page = templet.render(templet.TEMPLATES.BASE, {title:'My Title', body:'Greetings'});
###Using http.ServerResponse.render
Templet also extends http.ServerResponse with render
method.
var base = templet.TEMPLATES.BASE;
http.addListener('request', function(req, res) {
res.render(base, {title:'My Title', body:new Date().toUTCString()});
});
###Building an element jQuery-style
Templet has also methods for creating an element with a chainable interface, a la jQuery.
var el = templet.createElement('div')
.id('mydiv')
.class('divver')
.getHTML();
You could use this generated element in some template:
var templet = require('templet');
var base = templet.TEMPLATES.BASE;
var date = templet.createElement('div')
.attr('id', 'date')
.text(new Date().toUTCString())
.getHTML();
var page = templet.render(base, {title:'My Title', body:date});
You can also use appendChild
and appendTo
for building some HTML.
var date = templet.createElement('div').text(new Date().toUTCString());
var container = templet.createElement('section').appendChild(date);
... but we should stop before replicating the entire DOM.
###Use with caution
While basic string manipulation is fast enough, it's generally more efficient to use Node as an AJAX JSON service for dynamic content.