Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added benchmark with append=false, added browser partials sample, rem…

…oved doU from the root folder
  • Loading branch information...
commit 89fd5d9134204ccc50df35500b16a3df3581d4b3 1 parent fb6ed19
@olado authored
View
19 benchmarks/templatesBench.js
@@ -14,12 +14,15 @@
}
function testsetup(snippet) {
- // doU (improved underscore) with 'it'
+ // doU with 'it'
var doUCompiled = doU.template(snippet);
- // doT (modified jQote2) with 'it'
+ // doT with 'it'
var doTCompiledParam = doT.template(snippet);
// doT with 'this'
var doTCompiled = doT.template(snippet.replace(/=it\./g, '=this.'));
+ // doT with 'it' and append = false
+ doT.templateSettings.append = false;
+ var doTCompiledNoAppend = doT.template(snippet);
jslitmus.test('doU.js', function() {
doUCompiled(data);
@@ -50,7 +53,17 @@
doTCompiledParam(data);
}
});
- }
+
+ jslitmus.test('doT.js - append off', function() {
+ doTCompiledNoAppend(data);
+ });
+
+ jslitmus.test('doT.js - append off - looping', function(count) {
+ while (count--) {
+ doTCompiledNoAppend(data);
+ }
+ });
+}
function runTests() {
//var sys = require('sys');
View
56 doU.js
@@ -1,56 +0,0 @@
-// doU.js
-// (c) 2011, Laura Doktorova
-// https://github.com/olado/doT
-//
-// doU is an extraction and slight modification of an excellent
-// templating function from jQote2.js (jQuery plugin) by aefxx
-// (http://aefxx.com/jquery-plugins/jqote2/).
-//
-// Modifications:
-// 1. nodejs support
-// 2. allow for custom template markers
-// 3. only allow direct invocation of the compiled function
-//
-// Licensed under the MIT license.
-
-(function() {
- var doU = { version : '0.1.2' };
-
- if (typeof module !== 'undefined' && module.exports) {
- module.exports = doU;
- } else {
- this.doU = doU;
- }
-
- doU.templateSettings = {
- begin : '{{',
- end : '}}',
- varname : 'it'
- };
-
- doU.template = function(tmpl, conf) {
- conf = conf || doU.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);
-
- 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, '&#38;').split('<').join('&#60;').split('>').join('&#62;').split('" + '"' + "').join('&#34;').split(" + '"' + "'" + '"' + ").join('&#39;').split('/').join('&#x2F;');" : ';' + 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);
- } catch (e) {
- if (typeof console !== 'undefined') console.log("Could not create a template function: " + str);
- throw e;
- }
- };
-}());
View
54 examples/browsersample.html
@@ -0,0 +1,54 @@
+<html>
+ <head>
+
+ <script id="headertmpl" type="text/dot-template">
+ <h1>{{=it.title}}</h1>
+ </script>
+
+ <script id="pagetmpl" type="text/x-dot-template">
+ <h2>Here is the page using a header template</h2>
+ {{#def.header}}
+ {{=it.name}}
+ </script>
+
+ <script id="customizableheadertmpl" type="text/x-dot-template">
+ {{#def.header}}
+ {{#def.mycustominjectionintoheader || ''}}
+ </script>
+
+ <script id="pagetmplwithcustomizableheader" type="text/x-dot-template">
+ <h2>Here is the page with customized header template</h2>
+ {{##def.mycustominjectionintoheader:
+ <div>{{=it.title}} is not {{=it.name}}</div>
+ #}}
+ {{#def.customheader}}
+ {{=it.name}}
+ </script>
+
+ <script src="../doT.js" type="text/javascript"></script>
+ </head>
+
+ <body>
+ <div id="content"></div>
+ <div id="contentcustom"></div>
+ </body>
+
+ <script type="text/javascript">
+ var def = {
+ header: document.getElementById('headertmpl').text,
+ customheader: document.getElementById('customizableheadertmpl').text
+ };
+ var data = {
+ title: "My title",
+ name: "My name"
+ };
+
+ var pagefn = doT.template(document.getElementById('pagetmpl').text, undefined, def);
+ document.getElementById('content').innerHTML = pagefn(data);
+
+ pagefn = doT.template(document.getElementById('pagetmplwithcustomizableheader').text, undefined, def);
+ document.getElementById('contentcustom').innerHTML = pagefn(data);
+
+ </script>
+
+</html>
View
17 examples/customdoU.js
@@ -1,17 +0,0 @@
-(function() {
- var doU = require('../doU.js'),
- data = { f1: 1, f2: 2, f3: 3},
- snippet = '<h1>Just static text</h1> <p>Here is a simple case <?=it.f1+it.f3?> </p> <div class="<?=it.f1?>"> Next we will use a JavaScript block: </div> <? for(var i=0; i < it.f2; i++) { ?> <div><?=it.f3?></div> <? }; ?>';
-
- doU.templateSettings = {
- begin : '<?',
- end : '?>',
- varname : 'it'
- };
-
-
- var doUCompiled = doU.template(snippet);
-
- console.log("Generated function: \n" + doUCompiled.toString());
- console.log("Result of calling with " + JSON.stringify(data) + " :\n" + doUCompiled(data));
-}());
View
14 examples/withdoU.js
@@ -1,14 +0,0 @@
-(function() {
- var doU = require('../doU.js'),
- data = { f1: 1, f2: 2, f3: 3};
-
- require('fs').readFile(process.argv[1].replace(/\/[^\/]*$/,'/snippet.txt'), function (err, snippet) {
- if (err) {
- console.log("Error reading snippet.txt " + err);
- } else {
- var doUCompiled = doU.template(snippet.toString());
- console.log("Generated function: \n" + doUCompiled.toString());
- console.log("Result of calling with " + JSON.stringify(data) + " :\n" + doUCompiled(data));
- }
- });
-}());
Please sign in to comment.
Something went wrong with that request. Please try again.