4 ms·
What's the benefit over a regular templating language ? Can't you do all that with jinja2 ?
by sametmax 8y ago
What's the benefit over a regular templating language ? Can't you do all that with jinja2 ?
- bryanlarsen 8y agoManipulating 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.
- blattimwind 8y ago+1, not everything is meant to go through the 11th iteration of the regex-meatgrinder.
- epage 8y agoAnother +1. I was recently contemplating a problem that could be aided by what I was calling strucural templating and almost wrote something like this.
- emidln 8y agoThe 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.