Hacker News new | past | comments | ask | show | jobs | submit login

Manipulating structured data using a text templating language is best left to masochists.

For example, merging two objects is trivial in jsonnet yet pretty close to impossible in jinja2.




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. }}


Sure, 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.


You 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.


That 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.


+1, not everything is meant to go through the 11th iteration of the regex-meatgrinder.


Another +1. I was recently contemplating a problem that could be aided by what I was calling strucural templating and almost wrote something like this.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: