Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Forked from jonathantneal/templatejs.js
Created February 23, 2012 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbranyen/1895213 to your computer and use it in GitHub Desktop.
Save tbranyen/1895213 to your computer and use it in GitHub Desktop.
templatejs.js
// TemplateJS v3.1.1 MIT/GPL2 @jon_neal
(function (global) {
function escapeJS (str) {
return str.replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r');
}
function TemplateWalk (instance, str) {
// check for the opening and closer strings
var indexOpener = str.indexOf(instance.chars.open);
var indexCloser = str.indexOf(instance.chars.close);
// init our array buffer
var buffer = [];
// if the opening string is encountered
if (indexOpener > -1 && indexOpener < indexCloser) {
buffer.push(instance.chars.buffer + '.push("' + escapeJS(str.substr(0, indexOpener)) + '")');
buffer.push(TemplateWalk(instance, str.substr(indexOpener + instance.chars.open.length)));
}
// if the closing string is encountered
else if (indexCloser > -1) {
var firstChar = str.substr(0, 1);
if (instance.helpers[firstChar]) {
buffer.push(instance.helpers[firstChar](str.substr(1, indexCloser - 1)));
buffer.push(TemplateWalk(instance, str.substr(indexCloser + instance.chars.close.length)));
} else {
buffer.push(str.substr(0, indexCloser));
buffer.push(TemplateWalk(instance, str.substr(indexCloser + instance.chars.close.length)));
}
}
// if a regular string encountered
else {
buffer.push(instance.chars.buffer + '.push("' + escapeJS(str) + '")');
}
return buffer.join(';');
}
function Template () {
var instance = this, compiled;
instance.chars = {
'open': '<%',
'close': '%>',
'buffer': '__out__',
'property': '__property__'
};
instance.helpers = {
'=': function (js) {
return instance.chars.buffer + '.push(' + js + ')';
},
'?': function (js) {
return 'if(true){if((' + js + ')){';
},
'!': function (js) {
return 'if(true){if(!(' + js + ')){';
},
'#': function (js) {
return 'for(var ' + instance.chars.property + ' in ' + js + '){with(' + js + '[' + instance.chars.property + ']){';
},
'@': function (js) {
return 'if(true){with(' + js + '){';
},
'/': function (js) {
return '}}';
}
};
instance.data = {
source: '',
scope: {},
render: function() {}
};
return this;
}
Template.prototype.source = function (val) {
this.data.source = String(val);
this.data.render = Function(this.chars.buffer + '=[];with(this){' + TemplateWalk(this, this.data.source) + '}return ' + this.chars.buffer + '.join("")');
};
Template.prototype.scope = function (val) {
this.data.scope = val;
};
Template.prototype.render = function () {
return this.data.render.call(this.data.scope);
};
global.Template = Template;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment