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

Loading…
Cancel
Save