templet

0.0.3 • Public • Published

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').createTemplet();
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 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.createTemplet().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.

Readme

Keywords

none

Package Sidebar

Install

npm i templet

Weekly Downloads

2

Version

0.0.3

License

none

Last publish

Collaborators

  • Weltschmerz