From 06a8543bcbac956149edf0bec5f513a8ddb8975f Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Wed, 12 Jan 2022 17:22:51 -0500 Subject: [PATCH] json support --- webtemplate.d | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/webtemplate.d b/webtemplate.d index d37f03e..f2b29f9 100644 --- a/webtemplate.d +++ b/webtemplate.d @@ -26,11 +26,18 @@ + + ``` Functions available: `encodeURIComponent`, `formatDate`, `dayOfWeek`, `formatTime` + + History: + Things inside script tag were added on January 7, 2022. +/ module arsd.webtemplate; @@ -261,6 +268,32 @@ void expandTemplate(Element root, var context) { } } asp.replaceWith(fragment); + } else if(ele.tagName == "script") { + auto source = ele.innerHTML; + string newCode; + check_more: + auto idx = source.indexOf("<%="); + if(idx != -1) { + newCode = source[0 .. idx]; + auto remaining = source[idx + 3 .. $]; + idx = remaining.indexOf("%>"); + if(idx == -1) + throw new Exception("unclosed asp code in script"); + auto code = remaining[0 .. idx]; + + auto data = interpret(code, context); + newCode ~= data.toJson(); + + source = remaining[idx + 2 .. $]; + goto check_more; + } + + if(newCode is null) + {} // nothing needed + else { + newCode ~= source; + ele.innerRawSource = newCode; + } } else { expandTemplate(ele, context); }