a template module for node.js
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 1 commit behind jed:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



NOTE: This library has been updated to be compatible with the latest nodejs(0.1.100);

tmpl-node is a template module for node.js, based on John Resig's approach to micro-templating. It's a lightweight way to add templating to any node.js app.

To start, require the library in your app:

var tmpl = require( "./tmpl-node" );

Then, you can start creating your own templates:

var myTemplate = tmpl.compile("My name is <%= name %>.")

This lets you render the template within a given context, like this:

myTemplate({ name: "Jed" }) // => "My name is Jed."

You can render templates with multiple contexts, which will all be combined into one context for rendering

myTemplate = tmpl.compile("<%= app %> is running with pid <%= process.pid %>.")
myTemplate({ app: "This app" }, GLOBAL ) // => "This app is running with pid 1234."

You can also load templates in batch from a folder like this:

tmpl.load("./myTemplates/", function(tmpl){})

which creates a named template for each file, and takes a callback, so you can ensure everything is loaded properly. So if the folder had a file named page.html, you could render its contents like this:

tmpl[ "page.html" ]({ title: "Welcome!" })

Since this is probably the most useful for rendering server responses, the method render is added to the ServerResponse object, so that you can create handlers like this:

function( req, res ) {
  res.sendHeader(200, {"Content-Type": "text/html"});	
  res.render( "page.html", req, GLOBAL );

In this case, the render method renders the template named page.html with the request and GLOBAL objects combined into one context, uses sendBody to send it, and then calls finish() on the response object.

You can also add objects to the module's defaultContexts array, so that they're automatically added to the context whenever a template is rendered:

tmpl.defaultContexts.push( GLOBAL )

That's about it. Send any questions or comments here.