atemplate

Very simple template organizer for Node.JS

npm install atemplate
1 downloads in the last day
4 downloads in the last week
6 downloads in the last month

ATemplate

Very simple template organizer for Node.js

It asynchroniously renders the templates via the mustache or user-supplied function.

I wrote it because of template inheritance support lack in the mustache. Which (lack itself) is a very bright idea, I think.

But my web-pages consists of many components that should be choosen at runtime and rendered independently and (I prefer) asynchroniously.

Feel free to use, ask questions and contribute =)

Installation

npm install atemplate

Setup

var ATOptions = {
    "renderFuncType": "callback",
    "renderFunc": function (template, data, callback) {
        do_something_async_with_data_and_template(template, data, function (err, newData) {
            if (err) return callback(err);
            callback(null, newData);
        });
    }
}

var ATemplate = require("ATemplate")("/path/to/views", ATOptions);

First module initializer function argument is required and sets path to the template base directory.

Second parameter is optional and must be a dictionary, containing renderFunc and renderFuncType settings.

renderFuncType

renderFuncType should be "return" or "callback"

renderFunc

Rendering function should take two or three arguments (depending on rederFuncType)

  1. template file contents
  2. data source
  3. callback function (traditionally with "err, results" interface) — only in case of rederFuncType set to "callback"

Data source

Data source can be dictionary, function that returns dictionary or ATemplate instance (which makes most of the magic).

Again: datasource can't be anything else than dictionary or another ATemplate instance.

Return value

Third parameter is required if renderFuncType is set to "callback". Otherwise rendering function should return a string value via the return statement.

Code example

This is how I user ATemplate with the default mustache rendering

var ATemplate = require("ATemplate")(app.get("views"));

app.get('/login/', function (req, res) {
    new ATemplate("guest", {
        navbar: new ATemplate("navbar_guest"),
        content: new ATemplate("login_form")
    }).render(function (err, body) {
        if (err) throw new Error(err);
        res.send(body);
    });
});

License

MIT

npm loves you