Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion tinytim.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@

var start = "{{",
end = "}}",
allowed = "mod|even|odd",
path = "[a-z0-9_$][\\.a-z0-9_]*", // e.g. config.person.name
pattern = new RegExp(start + "\\s*("+ path +")\\s*" + end, "gi"),
functions = new RegExp( start + "\\s*@(" + allowed + ")\\s*(\\d+)*\\s*" + end + "([\\s\\S]+?)" + start + "\\s*@end(\\1)\\s*" + end , "gi"),
undef;

return function(template, data){
return function(template, data, index){
// Interpret allowed function tags in the templates
template = template.replace(functions,function(match, tag, criteria, content){
switch(tag.toLowerCase()) {
case 'even':
return ( index % 2 === 0 ) ? content : '';
case 'odd':
return ( index % 2 === 1 ) ? content : '';
case 'mod':
return ( index % criteria === 0 ) ? content : '';
}
return '' // should never get here but just in case;
});
// Merge data into the template string
return template.replace(pattern, function(tag, token){
var path = token.split("."),
Expand Down