Skip to content

Commit

Permalink
bumped version to 0.2.0, added selfcontained compilation option
Browse files Browse the repository at this point in the history
  • Loading branch information
olado committed Apr 22, 2012
1 parent ee9848b commit 48603e9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README
@@ -1,13 +1,14 @@
Created in search of the fastest and concise JavaScript templating function with emphasis on performance under V8 and nodejs. It shows great performance for both nodejs and browsers.

doT.js is a blend of jQote2 and underscore.js templating functions with lots of additions and performance improvements. It is fast, small and has no dependencies.
doT.js is fast, small and has no dependencies.

Features:
custom delimiters,
runtime evaluation,
runtime interpolation,
compile-time evaluation,
partials support,
conditionals,
encoding,
control whitespace - strip or preserve,
streaming friendly
Expand Down
20 changes: 13 additions & 7 deletions benchmarks/templating/doT.js
Expand Up @@ -7,7 +7,7 @@
(function() {
"use strict";

var doT = { version : '0.1.8' };
var doT = { version : '0.2.0' };

var global = (function () { return this || (0 || eval)('this'); }());

Expand All @@ -30,7 +30,8 @@
conditionalEnd: /\{\{\?\}\}/g,
varname: 'it',
strip : true,
append: true
append: true,
selfcontained: false
};

function resolveDefs(c, block, def) {
Expand Down Expand Up @@ -58,13 +59,14 @@
return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
}

if (!global.encodeHTML) { // encodeHTML is in global scope for performance :(
function encodeHTMLSource() {
var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
global.encodeHTML = function(code) {
return function(code) {
return code ? code.toString().replace(matchHTML, function(m) { return encodeHTMLRules[m] || m; }) : code;
};
}
global.encodeHTML = encodeHTMLSource();

var startend = { // optimal choice depends on platform/size of templates
append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
Expand All @@ -73,7 +75,7 @@

doT.template = function(tmpl, c, def) {
c = c || doT.templateSettings;
var cse = c.append ? startend.append : startend.split, str;
var cse = c.append ? startend.append : startend.split, str, needhtmlencode;

if (c.use || c.define) {
var olddef = global.def; global.def = def || {}; // workaround minifiers
Expand All @@ -88,14 +90,15 @@
return cse.start + unescape(code) + cse.end;
})
.replace(c.encode, function(m, code) {
needhtmlencode = true;
return cse.startencode + unescape(code) + cse.end;
})
.replace(c.conditionalEnd, "';}out+='")
.replace(c.conditionalElse, function(m, code) {
return (code) ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='";
return (code) ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='";
})
.replace(c.conditionalStart, function(m, code) {
return "';if(" + unescape(code) + "){out+='";
return "';if(" + unescape(code) + "){out+='";
})
.replace(c.evaluate, function(m, code) {
return "';" + unescape(code) + "out+='";
Expand All @@ -107,6 +110,9 @@
.split("out+='';").join('')
.split("var out='';out+=").join('var out=');

if (needhtmlencode && c.selfcontained) {
str = "var encodeHTML=("+ encodeHTMLSource.toString()+"());"+str;
}
try {
return new Function(c.varname, str);
} catch (e) {
Expand Down
20 changes: 13 additions & 7 deletions doT.js
Expand Up @@ -7,7 +7,7 @@
(function() {
"use strict";

var doT = { version : '0.1.8' };
var doT = { version : '0.2.0' };

var global = (function () { return this || (0 || eval)('this'); }());

Expand All @@ -30,7 +30,8 @@
conditionalEnd: /\{\{\?\}\}/g,
varname: 'it',
strip : true,
append: true
append: true,
selfcontained: false
};

function resolveDefs(c, block, def) {
Expand Down Expand Up @@ -58,13 +59,14 @@
return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
}

if (!global.encodeHTML) { // encodeHTML is in global scope for performance :(
function encodeHTMLSource() {
var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
global.encodeHTML = function(code) {
return function(code) {
return code ? code.toString().replace(matchHTML, function(m) { return encodeHTMLRules[m] || m; }) : code;
};
}
global.encodeHTML = encodeHTMLSource();

var startend = { // optimal choice depends on platform/size of templates
append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
Expand All @@ -73,7 +75,7 @@

doT.template = function(tmpl, c, def) {
c = c || doT.templateSettings;
var cse = c.append ? startend.append : startend.split, str;
var cse = c.append ? startend.append : startend.split, str, needhtmlencode;

if (c.use || c.define) {
var olddef = global.def; global.def = def || {}; // workaround minifiers
Expand All @@ -88,14 +90,15 @@
return cse.start + unescape(code) + cse.end;
})
.replace(c.encode, function(m, code) {
needhtmlencode = true;
return cse.startencode + unescape(code) + cse.end;
})
.replace(c.conditionalEnd, "';}out+='")
.replace(c.conditionalElse, function(m, code) {
return (code) ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='";
return (code) ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='";
})
.replace(c.conditionalStart, function(m, code) {
return "';if(" + unescape(code) + "){out+='";
return "';if(" + unescape(code) + "){out+='";
})
.replace(c.evaluate, function(m, code) {
return "';" + unescape(code) + "out+='";
Expand All @@ -107,6 +110,9 @@
.split("out+='';").join('')
.split("var out='';out+=").join('var out=');

if (needhtmlencode && c.selfcontained) {
str = "var encodeHTML=("+ encodeHTMLSource.toString()+"());"+str;
}
try {
return new Function(c.varname, str);
} catch (e) {
Expand Down
10 changes: 5 additions & 5 deletions doT.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 48603e9

Please sign in to comment.