From 1117fe4e3b6003a6315f773893e10834b58d2a49 Mon Sep 17 00:00:00 2001 From: Simon Harrer Date: Thu, 13 Jun 2024 17:32:50 +0200 Subject: [PATCH] Enable nested rendering --- README.md | 3 ++- public/templates.js | 15 ++++++++------- src/render.js | 3 +++ templates/datacontract.html | 2 +- templates/partials/model_field.html | 6 +++--- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9092299..edbae0c 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ cp public/templates.js dist/templates.js Templates are taken from the Data Contract CLI. To update the templates, do the following: 1. Copy the templates from `/datacontract-cli/datacontract/templates` to `public`: cp -r ../datacontract-cli/datacontract/templates public -2. remove .items() everywhere +2. remove .items() everywhere (in all templates) +3. replace "True" with "true", replace "False" with "false" 3. Fix datacontract.html so that it does not contain all the wrapper stuff around it 4. `npm run precompile` diff --git a/public/templates.js b/public/templates.js index 0dfa2ec..3ced54d 100644 --- a/public/templates.js +++ b/public/templates.js @@ -121,7 +121,7 @@ frame.set("loop.first", t_13 === 0); frame.set("loop.last", t_13 === t_14 - 1); frame.set("loop.length", t_14); output += "\n "; -output += runtime.suppressValue((lineno = 80, colno = 37, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": runtime.contextOrFrameLookup(context, frame, "False"),"field_name": t_16,"field": t_17,"level": 0})])), env.opts.autoescape); +output += runtime.suppressValue((lineno = 80, colno = 37, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": false,"field_name": t_16,"field": t_17,"level": 0})])), env.opts.autoescape); output += "\n "; ; } @@ -141,7 +141,7 @@ frame.set("loop.first", t_13 === 0); frame.set("loop.last", t_13 === t_14 - 1); frame.set("loop.length", t_14); output += "\n "; -output += runtime.suppressValue((lineno = 80, colno = 37, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": runtime.contextOrFrameLookup(context, frame, "False"),"field_name": t_18,"field": t_19,"level": 0})])), env.opts.autoescape); +output += runtime.suppressValue((lineno = 80, colno = 37, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": false,"field_name": t_18,"field": t_19,"level": 0})])), env.opts.autoescape); output += "\n "; ; } @@ -192,7 +192,7 @@ frame.set("loop.first", t_22 === 0); frame.set("loop.last", t_22 === t_23 - 1); frame.set("loop.length", t_23); output += "\n "; -output += runtime.suppressValue((lineno = 80, colno = 37, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": runtime.contextOrFrameLookup(context, frame, "False"),"field_name": t_25,"field": t_26,"level": 0})])), env.opts.autoescape); +output += runtime.suppressValue((lineno = 80, colno = 37, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": false,"field_name": t_25,"field": t_26,"level": 0})])), env.opts.autoescape); output += "\n "; ; } @@ -212,7 +212,7 @@ frame.set("loop.first", t_22 === 0); frame.set("loop.last", t_22 === t_23 - 1); frame.set("loop.length", t_23); output += "\n "; -output += runtime.suppressValue((lineno = 80, colno = 37, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": runtime.contextOrFrameLookup(context, frame, "False"),"field_name": t_27,"field": t_28,"level": 0})])), env.opts.autoescape); +output += runtime.suppressValue((lineno = 80, colno = 37, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": false,"field_name": t_27,"field": t_28,"level": 0})])), env.opts.autoescape); output += "\n "; ; } @@ -1078,7 +1078,7 @@ output += "\n \n \n\n\n"; if(runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"fields")) { output += "\n"; frame = frame.push(); -var t_7 = (lineno = 90, colno = 46, runtime.callWrap(runtime.memberLookup((runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"fields")),"items"), "field[\"fields\"][\"items\"]", context, [])); +var t_7 = runtime.memberLookup((runtime.contextOrFrameLookup(context, frame, "field")),"fields"); if(t_7) {t_7 = runtime.fromIterator(t_7); var t_5; if(runtime.isArray(t_7)) { @@ -1096,7 +1096,7 @@ frame.set("loop.first", t_5 === 0); frame.set("loop.last", t_5 === t_6 - 1); frame.set("loop.length", t_6); output += "\n "; -output += runtime.suppressValue((lineno = 91, colno = 19, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": runtime.contextOrFrameLookup(context, frame, "True"),"field_name": t_8,"field": t_9,"level": runtime.contextOrFrameLookup(context, frame, "level") + 1})])), env.opts.autoescape); +output += runtime.suppressValue((lineno = 91, colno = 19, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": true,"field_name": t_8,"field": t_9,"level": runtime.contextOrFrameLookup(context, frame, "level") + 1})])), env.opts.autoescape); output += "\n"; ; } @@ -1116,7 +1116,7 @@ frame.set("loop.first", t_5 === 0); frame.set("loop.last", t_5 === t_6 - 1); frame.set("loop.length", t_6); output += "\n "; -output += runtime.suppressValue((lineno = 91, colno = 19, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": runtime.contextOrFrameLookup(context, frame, "True"),"field_name": t_10,"field": t_11,"level": runtime.contextOrFrameLookup(context, frame, "level") + 1})])), env.opts.autoescape); +output += runtime.suppressValue((lineno = 91, colno = 19, runtime.callWrap(runtime.contextOrFrameLookup(context, frame, "render_partial"), "render_partial", context, ["partials/model_field.html",runtime.makeKeywordArgs({"nested": true,"field_name": t_10,"field": t_11,"level": runtime.contextOrFrameLookup(context, frame, "level") + 1})])), env.opts.autoescape); output += "\n"; ; } @@ -1126,6 +1126,7 @@ frame = frame.pop(); output += "\n\n\n\n"; ; } +output += "\n"; if(parentTemplate) { parentTemplate.rootRenderFunc(env, context, frame, runtime, cb); } else { diff --git a/src/render.js b/src/render.js index 9ea9b2d..dd8192e 100644 --- a/src/render.js +++ b/src/render.js @@ -9,6 +9,9 @@ const env = nunjucks.configure('templates', {autoescape: false}); env.addGlobal('render_partial', function (partialName, context) { return nunjucks.render(partialName, context); }); +env.addGlobal('range', function (from, to) { + return [...Array(to).keys()].map(i => i + from) +}); export function renderDataContract(dataContract) { return nunjucks.render("datacontract.html", {datacontract: dataContract}); diff --git a/templates/datacontract.html b/templates/datacontract.html index 6494d0b..2d9bb13 100644 --- a/templates/datacontract.html +++ b/templates/datacontract.html @@ -78,7 +78,7 @@

{% for field_name, field in model.fields %} - {{ render_partial('partials/model_field.html', nested = False, field_name=field_name, field = field, level = 0) }} + {{ render_partial('partials/model_field.html', nested = false, field_name=field_name, field = field, level = 0) }} {% endfor %} diff --git a/templates/partials/model_field.html b/templates/partials/model_field.html index 0218638..a0321b8 100644 --- a/templates/partials/model_field.html +++ b/templates/partials/model_field.html @@ -88,10 +88,10 @@ {% if field.fields %} -{% for field_name, field in field.fields.items() %} - {{ render_partial('partials/model_field.html', nested = True, field_name=field_name, field = field, level = level + 1) }} +{% for field_name, field in field.fields %} + {{ render_partial('partials/model_field.html', nested = true, field_name=field_name, field = field, level = level + 1) }} {% endfor %} -{% endif %} \ No newline at end of file +{% endif %}