mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 19:21:04 +01:00
templates.js added @first and @last conditionals
This commit is contained in:
@@ -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);
|
||||
function checkConditional(key, value) {
|
||||
var conditional = makeConditionalRegex(key),
|
||||
conditionalBlock = conditional.exec(template);
|
||||
|
||||
var conditionalBlock = conditional.exec(template);
|
||||
if (conditionalBlock !== null) {
|
||||
conditionalBlock = conditionalBlock[0].split(/<!-- ELSE -->/);
|
||||
|
||||
if (conditionalBlock !== null) {
|
||||
conditionalBlock = conditionalBlock[0].split(/<!-- ELSE -->/);
|
||||
if (conditionalBlock[1]) {
|
||||
// there is an else statement
|
||||
if (!value) {
|
||||
template = template.replace(conditional, conditionalBlock[1]);
|
||||
} else {
|
||||
template = template.replace(conditional, conditionalBlock[0]);
|
||||
}
|
||||
|
||||
if (conditionalBlock[1]) {
|
||||
// there is an else statement
|
||||
if (!data[d]) {
|
||||
template = template.replace(conditional, conditionalBlock[1]);
|
||||
} else {
|
||||
template = template.replace(conditional, conditionalBlock[0]);
|
||||
}
|
||||
|
||||
} else {
|
||||
// regular if
|
||||
if (!data[d]) {
|
||||
template = template.replace(conditional, '');
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user