templates.js added @first and @last conditionals

v1.18.x
psychobunny 11 years ago
parent 353b3047cd
commit 0e8b33aa79

@ -262,7 +262,7 @@
var template = this.html, var template = this.html,
regex, block; regex, block;
return (function parse(data, namespace, template) { return (function parse(data, namespace, template, blockInfo) {
if (!data || data.length == 0) { if (!data || data.length == 0) {
template = ''; template = '';
} }
@ -289,7 +289,7 @@
result = ""; result = "";
do { do {
result += parse(data[d][i], namespace, block); result += parse(data[d][i], namespace, block, {iterator: i, total: numblocks});
} while (i++ < numblocks); } while (i++ < numblocks);
namespace = namespace.replace(d + '.', ''); namespace = namespace.replace(d + '.', '');
@ -304,29 +304,37 @@
block = parse(data[d], namespace, block); block = parse(data[d], namespace, block);
template = setBlock(regex, block, template); template = setBlock(regex, block, template);
} else { } else {
var conditional = makeConditionalRegex(namespace + d); function checkConditional(key, value) {
var conditional = makeConditionalRegex(key),
var conditionalBlock = conditional.exec(template); conditionalBlock = conditional.exec(template);
if (conditionalBlock !== null) {
conditionalBlock = conditionalBlock[0].split(/<!-- ELSE -->/);
if (conditionalBlock[1]) { if (conditionalBlock !== null) {
// there is an else statement conditionalBlock = conditionalBlock[0].split(/<!-- ELSE -->/);
if (!data[d]) {
template = template.replace(conditional, conditionalBlock[1]);
} else {
template = template.replace(conditional, conditionalBlock[0]);
}
} else { if (conditionalBlock[1]) {
// regular if // there is an else statement
if (!data[d]) { if (!value) {
template = template.replace(conditional, ''); template = template.replace(conditional, conditionalBlock[1]);
} else {
template = template.replace(conditional, conditionalBlock[0]);
}
} else {
// regular if
if (!value) {
template = template.replace(conditional, '');
}
} }
} }
} }
checkConditional(namespace + d, data[d]);
if (blockInfo) {
checkConditional('@first', blockInfo.iterator === 0);
checkConditional('@last', blockInfo.iterator === blockInfo.total);
}
template = replace(namespace + d, data[d], template); template = replace(namespace + d, data[d], template);
} }
} }

Loading…
Cancel
Save