Skip to content

Node.js based forum software built for the modern web

templates - basic implementation of conditionals

psychobunnybbb716723fc0
diff --git a/public/src/templates.js b/public/src/templates.js
index cb153ed10d..5e9d5e35f5 100644
--- a/public/src/templates.js
+++ b/public/src/templates.js
@@ -227,6 +227,10 @@
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) {
data = template.match(regex);
if (data == null) return;
@@ -240,6 +244,19 @@
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) {
return template.replace(regex, block);
}
@@ -289,6 +306,14 @@
block = parse(data[d], namespace, block);
template = setBlock(regex, block, template);
} 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);
}
}