+38
-32
| // doT.js | ||
| // (c) 2011, Laura Doktorova | ||
| // 2011, Laura Doktorova | ||
| // https://github.com/olado/doT | ||
| // | ||
| // doT is an extraction and slight modification of an excellent | ||
| // templating function from jQote2.js (jQuery plugin) by aefxx | ||
| // (http://aefxx.com/jquery-plugins/jqote2/). | ||
| // doT is a blend of templating functions from jQote2.js | ||
| // (jQuery plugin) by aefxx (http://aefxx.com/jquery-plugins/jqote2/) | ||
| // and underscore.js (http://documentcloud.github.com/underscore/) | ||
| // | ||
| // Modifications: | ||
| // 1. nodejs support | ||
| // 2. allow for custom template markers | ||
| // 3. only allow direct invocation of the compiled function | ||
| // Licensed under the MIT license. | ||
| // | ||
| // Licensed under the MIT license. | ||
| (function() { | ||
| var doT = { version : '0.1.2' }; | ||
| var doT = { version : '0.1.3' }; | ||
@@ -26,27 +21,38 @@ if (typeof module !== 'undefined' && module.exports) { | ||
| doT.templateSettings = { | ||
| begin : '{{', | ||
| end : '}}', | ||
| varname : 'it' | ||
| evaluate : /\{\{([\s\S]+?)\}\}/g, | ||
| interpolate : /\{\{=([\s\S]+?)\}\}/g, | ||
| encode : /\{\{!([\s\S]+?)\}\}/g, | ||
| defines: /\{\{#([\s\S]+?)\}\}/g, | ||
| varname : 'it', | ||
| strip : true | ||
| }; | ||
| doT.template = function(tmpl, conf) { | ||
| conf = conf || doT.templateSettings; | ||
| var str = '', tb = conf.begin, te = conf.end, m, l, | ||
| arr = tmpl.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g, '') | ||
| .split(tb).join(te +'\x1b') | ||
| .split(te); | ||
| doT.template = function(tmpl, c, defs) { | ||
| c = c || doT.templateSettings; | ||
| var str = ("var out='" + | ||
| ((c.strip) ? tmpl.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g, ''): | ||
| tmpl) | ||
| .replace(c.defines, function(match, code) { | ||
| return eval(code.replace(/[\r\t\n]/g, ' ')); | ||
| }) | ||
| .replace(/\\/g, '\\\\') | ||
| .replace(/'/g, "\\'") | ||
| .replace(c.interpolate, function(match, code) { | ||
| return "';out+=" + code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ') + ";out+='"; | ||
| }) | ||
| .replace(c.encode, function(match, code) { | ||
| return "';out+=(" + code.replace(/\\'/g, "'").replace(/\\\\/g, "\\").replace(/[\r\t\n]/g, ' ') + ").toString().replace(/&(?!\\w+;)/g, '&').split('<').join('<').split('>').join('>').split('" + '"' + "').join('"').split(" + '"' + "'" + '"' + ").join(''').split('/').join('/');out+='"; | ||
| }) | ||
| .replace(c.evaluate, function(match, code) { | ||
| return "';" + code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ') + "out+='"; | ||
| }) | ||
| + "';return out;") | ||
| .replace(/\n/g, '\\n') | ||
| .replace(/\t/g, '\\t') | ||
| .replace(/\r/g, '\\r') | ||
| .split("out+='';").join('') | ||
| .split('var out="";out+=').join('var out='); | ||
| for (m=0,l=arr.length; m < l; m++) { | ||
| str += arr[m].charAt(0) !== '\x1b' ? | ||
| "out+='" + arr[m].replace(/(\\|["'])/g, '\\$1') + "'" : (arr[m].charAt(1) === '=' ? | ||
| ';out+=(' + arr[m].substr(2) + ');' : (arr[m].charAt(1) === '!' ? | ||
| ';out+=(' + arr[m].substr(2) + ").toString().replace(/&(?!\\w+;)/g, '&').split('<').join('<').split('>').join('>').split('" + '"' + "').join('"').split(" + '"' + "'" + '"' + ").join(''');" : ';' + arr[m].substr(1))); | ||
| } | ||
| str = ('var out="";'+str+';return out;') | ||
| .split("out+='';").join('') | ||
| .split('var out="";out+=').join('var out='); | ||
| try { | ||
| return new Function(conf.varname, str); | ||
| return new Function(c.varname, str); | ||
| } catch (e) { | ||
@@ -53,0 +59,0 @@ if (typeof console !== 'undefined') console.log("Could not create a template function: " + str); |
@@ -7,8 +7,10 @@ (function() { | ||
| doT.templateSettings = { | ||
| begin : '<?', | ||
| end : '?>', | ||
| varname : 'it' | ||
| evaluate : /\<\?([\s\S]+?)\?\>/g, | ||
| interpolate : /\<\?=([\s\S]+?)\?\>/g, | ||
| varname : 'it', | ||
| strip: true | ||
| }; | ||
| var doTCompiled = doT.template(snippet); | ||
@@ -15,0 +17,0 @@ |
+1
-1
@@ -10,3 +10,3 @@ { | ||
| ], | ||
| "version": "0.1.2", | ||
| "version": "0.1.3", | ||
| "main": "doT", | ||
@@ -13,0 +13,0 @@ "homepage": "http://github.com/olado/doT", |
+17
-13
| Created in search of the fast and concise JavaScript templating function with emphasis on performance under V8 and nodejs. | ||
| doT.js is based on templating function extracted from excellent jQuery plugin jQote2 with some modifications. | ||
| doT.js can be used with nodejs, allows for custom template delimiters and only allows direct invocation of the compiled function. | ||
| doT can be used with nodejs, allows for custom template delimiters and only allows direct invocation of the compiled function. | ||
| doT.js is a blend of jQote2 and underscore.js templating functions. | ||
| doT.js can be used with node or without. | ||
| In addition to performance gains, another advantage of jQote2 technique is that it allows you to flush data to the stream at any point in the generated template function, this is handy when using on server. | ||
| doT.js (using jQote2 technique) provides excellent performance under V8. | ||
| Note: previous version of doT.js 0.1.2 has been renamed to doU.js | ||
| Benchmarks: | ||
| Source: | ||
| http://olado.github.com/doT | ||
| Usage: | ||
| Default delimiters: | ||
| {{= }} for interpolation, | ||
| {{ }} for evaluation, | ||
| {{! }} for interpolation with encoding, | ||
| {{# }} for evaluation during compilation as opossed to runtime | ||
| Benchmark code is in to http://github.com/olado/doT/benchmarks | ||
| To run benchmarks with node: | ||
| node benchmarks/templatesBench.js | ||
| Other delimiters may be configured. | ||
| Usage: | ||
| doT.js by default uses {{= }} and {{ }} delimiters, though other delimiters may be configured. | ||
| doT.js also inherits jQote2' support for auto-encoding when ! is used. | ||
| To control whitespace use 'strip' option, true - to strip, false - to preserve. | ||
| Sample: | ||
| // 1. Compile template function | ||
@@ -26,3 +30,3 @@ var tempFn = doT.template("<h1>Here is a sample template {{=it.foo}} </h1>"); | ||
| License: | ||
| Licensed under the MIT License. (See LICENSE-DOT) |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
6124
12.14%80
15.94%32
14.29%2
100%