2 ms·
The following would be trivial to build in jinja2: {{ merge(foo, bar)|tojson|safe }} Even something polymorphic to render a template, then merge into a kn
by emidln 8y ago
The following would be trivial to build in jinja2:
{{ merge(foo, bar)|tojson|safe }}
Even something polymorphic to render a template, then merge into a known dict, then output json would be easy. A signature that might look like:
{{ render_json_dict(foo, "foo.json")|merge(bar)|tojson|safe. }}
- bryanlarsen 8y agoSure, if you render the entire JSON in a single pipeline you can do a merge. But then you only have a single pipeline. In the last jsonnet template I wrote, I merged a few keys into the top level, merged a few keys into a deeper level, and added some random characters to the end of a string value to make it unique. Trivial in jsonnet.
- sametmax 8y agoThat seem like a twisted use case though. I never seen anything requiring that. Given the rarity, i would just script it instead of introducing a whole new tech into my stack.
- emidln 8y agoYou could do this multiple times in the same top-level template if you're a masochist. Just use jinja2's set command to setup local variables in the template. You can do awfully complicated logic in jinja2, it's just that almost nobody will pass that garbage in code review. It gets kicked up to the python layer where, being a full data structure and regular code, it is easier to organize, comment, and test.