Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
mrolafsson committed May 25, 2011
2 parents cb4ff95 + 3a005bc commit 491f7d7
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 58 deletions.
6 changes: 3 additions & 3 deletions examples/solr.html
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ <h1>Tempo + Solr = True Love</h1>
<div data-template style="display: none;">
<!-- Search Results -->
<div class="column grid_8">
<h2>{{response.numFound}} results found</h2>
<h2>{{response.numFound|format}} results found</h2>

<ol>
<li data-template="response.docs">
Expand All @@ -272,15 +272,15 @@ <h3>{{title}}</h3>
<h3>Subjects</h3>
<ul>
<li data-template="facet_counts.facet_fields.subject">
<a href="#" onclick='addFilter("subject", __[0]);'>{{[0]}}</a> <span>{{[1]}}</span>
<a href="#" onclick='addFilter("subject", __[0]);'>{{[0]|truncate 30}}</a> <span>{{[1]|format}}</span>
</li>
</ul>
</li>
<li>
<h3>Languages</h3>
<ul>
<li data-template="facet_counts.facet_fields.language">
<a href="#" onclick='addFilter("language", __[0]);'>{{[0]}}</a> <span>{{[1]}}</span>
<a href="#" onclick='addFilter("language", __[0]);'>{{[0]|truncate 30}}</a> <span>{{[1]|format}}</span>
</li>
</ul>
</li>
Expand Down
153 changes: 105 additions & 48 deletions tempo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Tempo Template Engine 1.5
* Tempo Template Engine 1.6
*
* http://tempojs.com/
*/
Expand All @@ -21,6 +21,11 @@ TempoEvent.Types = {

var Tempo = (function (tempo) {

/*!
* Constants
*/
var NUMBER_FORMAT_REGEX = /(\d+)(\d{3})/;

/*!
* Helpers
*/
Expand All @@ -44,6 +49,7 @@ var Tempo = (function (tempo) {
}
return val;
},

trim : function (str) {
return str.replace(/^\s*([\S\s]*?)\s*$/, '$1');
},
Expand All @@ -53,12 +59,8 @@ var Tempo = (function (tempo) {
},

clearContainer : function (el) {
if (el !== undefined && el.childNodes !== undefined) {
for (var i = el.childNodes.length; i >= 0; i--) {
if (el.childNodes[i] !== undefined && el.childNodes[i].getAttribute !== undefined && el.childNodes[i].getAttribute('data-template') !== null) {
el.childNodes[i].parentNode.removeChild(el.childNodes[i]);
}
}
if (el !== undefined) {
el.innerHTML = '';
}
},

Expand Down Expand Up @@ -120,12 +122,33 @@ var Tempo = (function (tempo) {
}
};

function Templates(nestedItem) {
function Templates(params, nestedItem) {
this.params = params;
this.defaultTemplate = null;
this.namedTemplates = {};
this.container = null;

this.nestedItem = nestedItem !== undefined ? nestedItem : null;

this.var_brace_left = '\\{\\{';
this.var_brace_right = '\\}\\}';
this.tag_brace_left = '\\{%';
this.tag_brace_right = '%\\}';

if (typeof params !== 'undefined') {
for (var prop in params) {
if (prop === 'var_braces') {
this.var_brace_left = params[prop].substring(0, params[prop].length / 2);
this.var_brace_right = params[prop].substring(params[prop].length / 2);
} else if (prop === 'tag_braces') {
this.tag_brace_left = params[prop].substring(0, params[prop].length / 2);
this.tag_brace_right = params[prop].substring(params[prop].length / 2);
} else if (typeof this[prop] !== 'undefined') {
this[prop] = params[prop];
}
}
}

return this;
}

Expand Down Expand Up @@ -200,9 +223,9 @@ var Tempo = (function (tempo) {
}
},

templateFor: function (item) {
templateFor: function (i) {
for (var templateName in this.namedTemplates) {
if (eval('item.' + templateName)) {
if (eval('i.' + templateName)) {
return this.namedTemplates[templateName].cloneNode(true);
}
}
Expand All @@ -220,6 +243,8 @@ var Tempo = (function (tempo) {
this.templates = templates;
this.listener = undefined;
this.started = false;
this.varRegex = new RegExp(this.templates.var_brace_left + '[ ]?([A-Za-z0-9$\\._\\[\\]]*?)([ ]?\\|[ ]?.*?)?[ ]?' + this.templates.var_brace_right, 'g');
this.tagRegex = new RegExp(this.templates.tag_brace_left + '[ ]?([\\s\\S]*?)( [\\s\\S]*?)?[ ]?' + this.templates.tag_brace_right + '(([\\s\\S]*?)(?=' + this.templates.tag_brace_left + '[ ]?end\\1[ ]?' + this.templates.tag_brace_right + '))?', 'g');

return this;
}
Expand All @@ -231,8 +256,8 @@ var Tempo = (function (tempo) {
return this;
},

_replaceVariables : function (renderer, _tempo, item, str) {
return str.replace(/\{\{[ ]?([A-Za-z0-9$\._\[\]]*?)([ ]?\|[ ]?.*?)?[ ]?\}\}/g, function (match, variable, args) {
_replaceVariables : function (renderer, _tempo, i, str) {
return str.replace(this.varRegex, function (match, variable, args) {
try {
var val = null;

Expand All @@ -241,18 +266,18 @@ var Tempo = (function (tempo) {
return eval(variable);
}

if (utils.typeOf(item) === 'array') {
val = eval('item' + variable);
if (utils.typeOf(i) === 'array') {
val = eval('i' + variable);
} else {
val = eval('item.' + variable);
val = eval('i.' + variable);
}

// Handle filters
var filterSplitter = new RegExp('\\|[ ]?(?=' + utils.memberRegex(renderer.filters) + ')', 'g');
if (args !== undefined && args !== '') {
var filters = utils.trim(utils.trim(args).substring(1)).split(filterSplitter);
for (var i = 0; i < filters.length; i++) {
var filter = utils.trim(filters[i]);
for (var p = 0; p < filters.length; p++) {
var filter = utils.trim(filters[p]);
var filter_args = [];
// If there is a space, there must be arguments
if (filter.indexOf(' ') > -1) {
Expand All @@ -275,8 +300,8 @@ var Tempo = (function (tempo) {
});
},

_replaceObjects : function (renderer, _tempo, item, str) {
var regex = new RegExp('(?:__[\\.]?)((_tempo|\\[|' + utils.memberRegex(item) + ')([A-Za-z0-9$\\._\\[\\]]+)?)', 'g');
_replaceObjects : function (renderer, _tempo, i, str) {
var regex = new RegExp('(?:__[\\.]?)((_tempo|\\[|' + utils.memberRegex(i) + ')([A-Za-z0-9$\\._\\[\\]]+)?)', 'g');
return str.replace(regex, function (match, variable, args) {
try {
var val = null;
Expand All @@ -286,10 +311,10 @@ var Tempo = (function (tempo) {
return eval(variable);
}

if (utils.typeOf(item) === 'array') {
val = eval('item' + variable);
if (utils.typeOf(i) === 'array') {
val = eval('i' + variable);
} else {
val = eval('item.' + variable);
val = eval('i.' + variable);
}

if (val !== undefined) {
Expand All @@ -316,11 +341,11 @@ var Tempo = (function (tempo) {
},

_applyTags : function (renderer, item, str) {
return str.replace(/\{%[ ]?([\s\S]*?)( [\s\S]*?)?[ ]?%\}(([\s\S]*?)(?=\{%[ ]?end\1[ ]?%\}))?/g, function (match, tag, args, body) {
return str.replace(this.tagRegex, function (match, tag, args, body) {
if (renderer.tags.hasOwnProperty(tag)) {
args = args.substring(args.indexOf(' ')).replace(/^[ ]*|[ ]*$/g, '');
filter_args = args.split(/(?:[\'"])[ ]?,[ ]?(?:[\'"])/);
return renderer.tags[tag](item, match, filter_args, body);
var filter_args = args.split(/(?:['"])[ ]?,[ ]?(?:['"])/);
return renderer.tags[tag](renderer, item, match, filter_args, body);
} else {
return '';
}
Expand All @@ -336,21 +361,21 @@ var Tempo = (function (tempo) {
return this;
},

renderItem : function (renderer, tempo_info, item, fragment) {
var template = renderer.templates.templateFor(item);
if (template && item) {
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.ITEM_RENDER_STARTING, item, template));
renderItem : function (renderer, tempo_info, i, fragment) {
var template = renderer.templates.templateFor(i);
if (template && i) {
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.ITEM_RENDER_STARTING, i, template));

var nestedDeclaration = template.innerHTML.match(/data-template="(.*?)"/g);
if (nestedDeclaration) {
for (var i = 0; i < nestedDeclaration.length; i++) {
var nested = nestedDeclaration[i].match(/"(.*?)"/)[1];
for (var p = 0; p < nestedDeclaration.length; p++) {
var nested = nestedDeclaration[p].match(/"(.*?)"/)[1];

var t = new Templates(nested);
var t = new Templates(renderer.templates.params, nested);
t.parse(template);

var r = new Renderer(t);
r.render(eval('item.' + nested));
r.render(eval('i.' + nested));
}
}

Expand All @@ -359,29 +384,29 @@ var Tempo = (function (tempo) {
var html = template.innerHTML.replace(/%7B%7B/g, '{{').replace(/%7D%7D/g, '}}');

// Tags
html = this._applyTags(this, item, html);
html = this._applyTags(this, i, html);

// Content
html = this._replaceVariables(this, tempo_info, item, html);
html = this._replaceVariables(this, tempo_info, i, html);

// JavaScript objects
html = this._replaceObjects(this, tempo_info, item, html);
html = this._replaceObjects(this, tempo_info, i, html);

// Template class attribute
if (template.getAttribute('class')) {
template.className = this._replaceVariables(this, tempo_info, item, template.className);
template.className = this._replaceVariables(this, tempo_info, i, template.className);
}

// Template id
if (template.getAttribute('id')) {
template.id = this._replaceVariables(this, tempo_info, item, template.id);
template.id = this._replaceVariables(this, tempo_info, i, template.id);
}

html = this._applyAttributeSetters(this, item, html);
html = this._applyAttributeSetters(this, i, html);

fragment.appendChild(utils.getElement(template, html));

utils.notify(this.listener, new TempoEvent(TempoEvent.Types.ITEM_RENDER_COMPLETE, item, template));
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.ITEM_RENDER_COMPLETE, i, template));
}
},

Expand Down Expand Up @@ -455,15 +480,16 @@ var Tempo = (function (tempo) {
},

tags : {
'if' : function(item, match, args, body) {
var member_regex = utils.memberRegex(item);
'if' : function (renderer, i, match, args, body) {
var member_regex = utils.memberRegex(i);

var expr = args[0].replace(/&amp;/g, '&');
expr = expr.replace(new RegExp(member_regex, 'gi'), function (match) {
return 'item.' + match;
return 'i.' + match;
});

var blocks = body.split(/\{%[ ]?else[ ]?%\}/);
var blockRegex = new RegExp(renderer.templates.tag_brace_left + '[ ]?else[ ]?' + renderer.templates.tag_brace_right, 'g');
var blocks = body.split(blockRegex);

if (eval(expr)) {
return blocks[0];
Expand All @@ -476,6 +502,35 @@ var Tempo = (function (tempo) {
},

filters : {
'truncate' : function (value, args) {
if (value !== undefined) {
var len = 0;
var rep = '...';
if (args.length > 0) {
len = parseInt(args[0]);
}
if (args.length > 1) {
rep = args[1];
}
if (value.length > len - 3) {
return value.substr(0, len - 3) + '...';
}
return value;
}
},
'format' : function (value, args) {
if (value !== undefined) {
var x = (value + '').split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';

while (NUMBER_FORMAT_REGEX.test(x1)) {
x1 = x1.replace(NUMBER_FORMAT_REGEX, '$1' + ',' + '$2');
}

return x1 + x2;
}
},
'upper' : function (value, args) {
return value.toUpperCase();
},
Expand Down Expand Up @@ -612,19 +667,21 @@ var Tempo = (function (tempo) {
* Prepare a container for rendering, gathering templates and
* clearing afterwards.
*/
tempo.prepare = function (container) {
tempo.prepare = function (container, params) {
if (typeof container === 'string') {
container = document.getElementById(container);
}

var templates = new Templates();
var templates = new Templates(params);
templates.parse(container);

return new Renderer(templates);
};

tempo.test = function () {
return { 'utils' : utils, 'renderer' : new Renderer(null) };
tempo.test = {
'utils' : utils,
'templates': new Templates({}),
'renderer' : new Renderer(new Templates({}))
};

return tempo;
Expand Down
Loading

0 comments on commit 491f7d7

Please sign in to comment.