webtemplate skeleton uda

This commit is contained in:
Adam D. Ruppe 2022-03-07 16:25:28 -05:00
parent 0faa1d3b8b
commit 369c40fd6f
2 changed files with 18 additions and 5 deletions

View File

@ -9731,6 +9731,7 @@ bool runInGuiThread(scope void delegate() dg) @trusted {
return true; return true;
} }
// note it runs sync if this is the gui thread....
void runInGuiThreadAsync(void delegate() dg, void delegate(Exception) nothrow handleError = null) nothrow { void runInGuiThreadAsync(void delegate() dg, void delegate(Exception) nothrow handleError = null) nothrow {
claimGuiThread(); claimGuiThread();

View File

@ -139,7 +139,7 @@ void addDefaultFunctions(var context) {
context.data = var.emptyObject; context.data = var.emptyObject;
} }
Document renderTemplate(string templateName, var context = var.emptyObject, var skeletonContext = var.emptyObject) { Document renderTemplate(string templateName, var context = var.emptyObject, var skeletonContext = var.emptyObject, string skeletonName = null) {
import std.file; import std.file;
import arsd.cgi; import arsd.cgi;
@ -147,7 +147,10 @@ Document renderTemplate(string templateName, var context = var.emptyObject, var
addDefaultFunctions(context); addDefaultFunctions(context);
addDefaultFunctions(skeletonContext); addDefaultFunctions(skeletonContext);
auto skeleton = new Document(readText("templates/skeleton.html"), true, true); if(skeletonName.length == 0)
skeletonName = "skeleton.html";
auto skeleton = new Document(readText("templates/" ~ skeletonName), true, true);
auto document = new Document(); auto document = new Document();
document.parseSawAspCode = (string) => true; // enable adding <% %> to the dom document.parseSawAspCode = (string) => true; // enable adding <% %> to the dom
document.parse("<root>" ~ readText("templates/" ~ templateName) ~ "</root>", true, true); document.parse("<root>" ~ readText("templates/" ~ templateName) ~ "</root>", true, true);
@ -265,7 +268,15 @@ void expandTemplate(Element root, var context) {
document.parseSawAspCode = (string) => true; // enable adding <% %> to the dom document.parseSawAspCode = (string) => true; // enable adding <% %> to the dom
document.parse("<root>" ~ readText("templates/" ~ templateName) ~ "</root>", true, true); document.parse("<root>" ~ readText("templates/" ~ templateName) ~ "</root>", true, true);
expandTemplate(document.root, context); var obj = var.emptyObject;
obj.prototype = context;
// FIXME: there might be other data you pass from the parent...
if(auto data = ele.getAttribute("data")) {
obj["data"] = var.fromJson(data);
}
expandTemplate(document.root, obj);
auto fragment = new DocumentFragment(null); auto fragment = new DocumentFragment(null);
@ -457,6 +468,7 @@ struct RenderTemplate {
string name; string name;
var context = var.emptyObject; var context = var.emptyObject;
var skeletonContext = var.emptyObject; var skeletonContext = var.emptyObject;
string skeletonName;
} }
@ -510,7 +522,7 @@ template WebPresenterWithTemplateSupport(CTRP) {
void presentSuccessfulReturnAsHtml(T : RenderTemplate)(Cgi cgi, T ret, Meta meta) { void presentSuccessfulReturnAsHtml(T : RenderTemplate)(Cgi cgi, T ret, Meta meta) {
addContext(cgi, ret.context); addContext(cgi, ret.context);
auto skeleton = renderTemplate(ret.name, ret.context, ret.skeletonContext); auto skeleton = renderTemplate(ret.name, ret.context, ret.skeletonContext, ret.skeletonName);
cgi.setResponseContentType("text/html; charset=utf8"); cgi.setResponseContentType("text/html; charset=utf8");
cgi.gzipResponse = true; cgi.gzipResponse = true;
cgi.write(skeleton.toString(), true); cgi.write(skeleton.toString(), true);
@ -537,7 +549,7 @@ template WebPresenterWithTemplateSupport(CTRP) {
obj.meta.currentPath = cgi.pathInfo; obj.meta.currentPath = cgi.pathInfo;
obj.meta.automaticForm = { return meta.automaticForm(this).toString; }; obj.meta.automaticForm = { return meta.automaticForm(this).toString; };
presentSuccessfulReturnAsHtml(cgi, RenderTemplate(meta.templateName, obj, sobj), meta); presentSuccessfulReturnAsHtml(cgi, RenderTemplate(meta.templateName, obj, sobj, meta.skeletonName), meta);
} else } else
super.presentSuccessfulReturnAsHtml(cgi, ret, meta); super.presentSuccessfulReturnAsHtml(cgi, ret, meta);
} }