A simple, lightweight, efficient and non-invasive pure-JavaScript templating engine

Node.js Module

Together with Node.js, a robust server-side JavaScript implementation, Kruntch.js enables a myriad of powerful and interesting templating possibilities from server-side HTML generation to in-line data transformation and pruning.
The Kruntch.js module for Node.js allows you to take full advantage of powerful templating functionality right in your Node.js backend.

From HTML generation to data transformation, Kruntch.js, as a simple, single purpose and focused tool, offers many possibilities for building flexible back-end systems.

This following node script enhances the standard "Hello World" Node.js example to generate response HTML using Kruntch.js:
// Require the HTTP server and the Kruntch.js template engine
var http = require('http');
var Kruntch = require('./kruntch.node.js');

// The Kruntch.js template definition
var template = "<html>" +
"<head>" +
"<title>[Title]!</title>" +
"</head>" +
"<body>" +
"<for each='Things'>[$] and <last>[$]</last></for> say: " +
"<br />" +
"<h1>[Say]</h1>" +
"<body>"
"</html>";

// The JavaScript object
var object = { Title: 'Hello World', Things: ['Thing One', 'Thing Two'], Say: 'Hello WORLD!' };

// Spin up the HTTP server
http.createServer(function (req, res) {

// Render response HTML
Kruntch.Apply(
template,
object,
function(html) {

// Write the response
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);

}
);

}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Another powerful use of Kruntch.js with Node.js is in performing basic in-line data transformation and/or data pruning.

In this respect, Kruntch.js is not being leveraged to generate HTML but rather some other text-based data format such as JSON or XML.

Data from a target system can be reuested and transformed/pruned into a new format resulting in a data-set that is more suitable for a particular application.

This following node script pulls the Twitter feed for the screen name specified in the query-string and then transforms it from its native JSON format into a simplified and pruned XML equivlent:
// Require the HTTP server, URL class and the Kruntch.js template engine
var http = require('http');
var url = require('url');
var Kruntch = require('./kruntch.node.js');

// The Kruntch.js template definition
var template = "<Feed screenName='[ScreenName]' backgroundColor='#[BackgroundColor]' >" +
"<for each='Tweets'>" +
"<Tweet>" +
"<ID>[TweetID]</ID≶" +
"<Text>[TweetText]</Text>" +
"<ScreenName>[TweetProfileScreenName]</ScreenName>" +
"<ProfileImageURL>[TweetProfileImageURL]</ProfileImageURL>" +
"</Tweet>" +
"{{" +
" OriginalTweet: function () {" +
" return (this.retweeted_status != undefined) ? this.retweeted_status : this;" +
" }," +
" TweetID: function () {" +
" return this.OriginalTweet().id_str;" +
" }," +
" TweetText: function () {" +
" return this.OriginalTweet().text;" +
" }," +
" TweetProfileImageURL: function () {" +
" return this.OriginalTweet().user.profile_image_url;" +
" }," +
" TweetProfileScreenName: function () {" +
" return this.OriginalTweet().user.screen_name;" +
" }\n" +
"}}" +
"</for>\n" +
"{{" +
" ScreenName: function () {" +
" return this.Tweets[0].user.screen_name;" +
" }," +
" BackgroundColor: function () {" +
" return this.Tweets[0].user.profile_background_color;" +
" }\n" +
"}}" +
"</Feed>";

// Spin up the HTTP server
http.createServer(function (req, res) {

// Parse the screen-name from the query-string
var sn = (url.parse(req.url, true)).query.sn;

// HTTP "Get" Twitter Feed JSON data
http.get("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=" + sn, function(response) {

// The response JSON data
var data = '';

// Handle the "data" response
response.on('data', function (chunk) {
data += chunk;
});

// Handle the "end" of data response
response.on('end',function() {

// Parse the data into a JavaScript object
var obj = JSON.parse(data);

// Render response HTML
Kruntch.Apply(
template,
{ Tweets: obj },
function(xml) {

// Write the response
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(xml);

}
);

});

});

}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Which results in the following XML:

<Feed screenName="java" backgroundColor="#C0DEED">
<Tweet>
<ID>331482049636626432</ID>
<Text>
Easy to be published in Java Magazine, submit a Fix This challenge...
</Text>
<ScreenName>java</ScreenName>
<ProfileImageURL>
http://a0.twimg.com/profile_images/770950120/java-icon48b_normal.png
</ProfileImageURL>
</Tweet>
<Tweet>
<ID>331439754035986435</ID>
<Text>
Need tech help? How To Ask Questions The Smart Way by ...
</Text>
<ScreenName>java</ScreenName>
<ProfileImageURL>
http://a0.twimg.com/profile_images/770950120/java-icon48b_normal.png
</ProfileImageURL>
</Tweet>

... SHORTENED FOR BREVITY ...

</Feed>