mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 11:11:04 +01:00
templates - basic implementation of conditionals
<!-- IF variable --> show me <!-- ENDIF variable -->
This commit is contained in:
@@ -227,6 +227,10 @@
|
|||||||
return new RegExp("<!-- BEGIN " + block + " -->[\\s\\S]*<!-- END " + block + " -->", 'g');
|
return new RegExp("<!-- BEGIN " + block + " -->[\\s\\S]*<!-- END " + block + " -->", 'g');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function makeConditionalRegex(block) {
|
||||||
|
return new RegExp("<!-- IF " + block + " -->[\\s\\S]*<!-- ENDIF " + block + " -->", 'g');
|
||||||
|
}
|
||||||
|
|
||||||
function getBlock(regex, block, template) {
|
function getBlock(regex, block, template) {
|
||||||
data = template.match(regex);
|
data = template.match(regex);
|
||||||
if (data == null) return;
|
if (data == null) return;
|
||||||
@@ -240,6 +244,19 @@
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getConditionalBlock(regex, block, template) {
|
||||||
|
data = template.match(regex);
|
||||||
|
if (data == null) return;
|
||||||
|
|
||||||
|
if (self.blocks && block !== undefined) self.blocks[block] = data[0];
|
||||||
|
|
||||||
|
data = data[0]
|
||||||
|
.replace("<!-- IF " + block + " -->", "")
|
||||||
|
.replace("<!-- ENDIF " + block + " -->", "");
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
function setBlock(regex, block, template) {
|
function setBlock(regex, block, template) {
|
||||||
return template.replace(regex, block);
|
return template.replace(regex, block);
|
||||||
}
|
}
|
||||||
@@ -289,6 +306,14 @@
|
|||||||
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(d),
|
||||||
|
block = getConditionalBlock(conditional, namespace, template);
|
||||||
|
|
||||||
|
if (block && !data[d]) {
|
||||||
|
template = template.replace(conditional, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template = replace(namespace + d, data[d], template);
|
template = replace(namespace + d, data[d], template);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user