Skip to content

Commit

Permalink
Added support for array iteration. Examples: {{~ it.arr :value:index …
Browse files Browse the repository at this point in the history
…}}, {{~ [1,2,3] :v}} etc.
  • Loading branch information
olado committed Apr 24, 2012
1 parent b470267 commit 95035e4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Features:
runtime interpolation,
compile-time evaluation,
partials support,
conditionals,
encoding,
conditionals,
array iteration,
control whitespace - strip or preserve,
streaming friendly

Expand Down
11 changes: 9 additions & 2 deletions benchmarks/templating/doT.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
encode: /\{\{!([\s\S]+?)\}\}/g,
use: /\{\{#([\s\S]+?)\}\}/g,
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\}\}/g,
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
varname: 'it',
strip : true,
append: true,
Expand Down Expand Up @@ -73,7 +74,7 @@

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

if (c.use || c.define) {
var olddef = global.def; global.def = def || {}; // workaround minifiers
Expand All @@ -96,6 +97,12 @@
(code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
(code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
})
.replace(c.iterate, function(m, iterate, vname, iname) {
if (!iterate) return "'} };out+='";
sid+=1;indv = iname || "i"+sid;iterate=unescape(iterate);
return "';var arr"+sid+"="+iterate+";if(arr"+sid+"){var "+indv+"=-1,l"+sid+"=arr"+sid+".length-1;while("+indv+"<l"+sid+"){"
+vname+"=arr"+sid+"["+indv+"+=1];out+='";
})
.replace(c.evaluate, function(m, code) {
return "';" + unescape(code) + "out+='";
})
Expand Down
11 changes: 9 additions & 2 deletions doT.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
encode: /\{\{!([\s\S]+?)\}\}/g,
use: /\{\{#([\s\S]+?)\}\}/g,
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\}\}/g,
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
varname: 'it',
strip : true,
append: true,
Expand Down Expand Up @@ -73,7 +74,7 @@

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

if (c.use || c.define) {
var olddef = global.def; global.def = def || {}; // workaround minifiers
Expand All @@ -96,6 +97,12 @@
(code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
(code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
})
.replace(c.iterate, function(m, iterate, vname, iname) {
if (!iterate) return "'} };out+='";
sid+=1;indv = iname || "i"+sid;iterate=unescape(iterate);
return "';var arr"+sid+"="+iterate+";if(arr"+sid+"){var "+indv+"=-1,l"+sid+"=arr"+sid+".length-1;while("+indv+"<l"+sid+"){"
+vname+"=arr"+sid+"["+indv+"+=1];out+='";
})
.replace(c.evaluate, function(m, code) {
return "';" + unescape(code) + "out+='";
})
Expand Down
11 changes: 6 additions & 5 deletions doT.min.js

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

29 changes: 24 additions & 5 deletions examples/advancedsnippet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,30 @@ Compare xyz to 1, show 'xyz is not 1' if false:

Conditionals:
{{? !it.altEmail }}
<p>
second email: {{= it.altEmail }}
</p>
<p>
second email: {{= it.altEmail }}
</p>
{{?? true }}
else case worked
else case worked
{{?}}


Array iterators
{{~ it.farray :p }}
<h1>{{=p.farray}}<h1>
{{~ p.farray :value:i }}
<h2>{{=i}}: {{=value}}</h2>
{{~ value :w }}
<h3>{{=w}}</h3>
{{~}}
{{~}}
{{~}}

{{~ ["apple", "banana", "orange"] :k}}
{{=k}}
{{~}}

{{~ (function(){ return [1,2,3]})() :k}}
{{=k}}
{{~}}

The end
2 changes: 1 addition & 1 deletion examples/withdoT.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function() {
var doT = require('../doT.js'),
fs = require('fs'),
data = { f1: 1, f2: 2, f3: 3, altEmail: "conditional works"},
data = { f1: 1, f2: 2, f3: 3, altEmail: "conditional works", farray:[{farray:[1,2,3,[11,22,33]],person:{name:'Ell',age:23}},{farray:{how:'really'}}, {farray:[5,6,7,8]}]},
defs = { a: 100, b: 200};

defs.loadfile = function(path) {
Expand Down

0 comments on commit 95035e4

Please sign in to comment.