Skip to content

Commit

Permalink
Enable nested rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
simonharrer committed Jun 13, 2024
1 parent 07df61e commit 1117fe4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
15 changes: 8 additions & 7 deletions public/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ";
;
}
Expand All @@ -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 ";
;
}
Expand Down Expand Up @@ -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 ";
;
}
Expand All @@ -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 ";
;
}
Expand Down Expand Up @@ -1078,7 +1078,7 @@ output += "\n </div>\n </td>\n</tr>\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)) {
Expand All @@ -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";
;
}
Expand All @@ -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";
;
}
Expand All @@ -1126,6 +1126,7 @@ frame = frame.pop();
output += "\n<!-- Mark the end of the contained fields -->\n<tr style=\"--tw-divide-y-reverse: 2\">\n</tr>\n";
;
}
output += "\n";
if(parentTemplate) {
parentTemplate.rootRenderFunc(env, context, frame, runtime, cb);
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
2 changes: 1 addition & 1 deletion templates/datacontract.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ <h1 class="text-base font-semibold leading-6 text-gray-900">
</thead>
<tbody class="divide-y divide-gray-200 bg-white">
{% 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 %}
</tbody>
</table>
Expand Down
6 changes: 3 additions & 3 deletions templates/partials/model_field.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@
</tr>

{% 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 %}
<!-- Mark the end of the contained fields -->
<tr style="--tw-divide-y-reverse: 2">
</tr>
{% endif %}
{% endif %}

0 comments on commit 1117fe4

Please sign in to comment.