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,
|
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,16 +304,16 @@
|
|||||||
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) {
|
if (conditionalBlock !== null) {
|
||||||
conditionalBlock = conditionalBlock[0].split(/<!-- ELSE -->/);
|
conditionalBlock = conditionalBlock[0].split(/<!-- ELSE -->/);
|
||||||
|
|
||||||
if (conditionalBlock[1]) {
|
if (conditionalBlock[1]) {
|
||||||
// there is an else statement
|
// there is an else statement
|
||||||
if (!data[d]) {
|
if (!value) {
|
||||||
template = template.replace(conditional, conditionalBlock[1]);
|
template = template.replace(conditional, conditionalBlock[1]);
|
||||||
} else {
|
} else {
|
||||||
template = template.replace(conditional, conditionalBlock[0]);
|
template = template.replace(conditional, conditionalBlock[0]);
|
||||||
@@ -321,11 +321,19 @@
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
// regular if
|
// regular if
|
||||||
if (!data[d]) {
|
if (!value) {
|
||||||
template = template.replace(conditional, '');
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user