Skip to content
Advertisement

Nested Dict to Table in Jinja2

I am scratching my head trying to correctly format a table from a nested dict in Jinja2, but having no luck.

The dict is something like this:

JavaScript

And I want the table to look like:

JavaScript

The closest I have come is:

JavaScript

But this doesn’t work. I am having trouble assigning the k1 variable without also having to extend the loop in a way that ruins the layout of the table. I would rather not have to rearrange the dict or split it into several dicts.

Can I convert a dict like the one I have to a table of that structure in Jinja2? If yes, how?

Advertisement

Answer

I did not test my solution, there might be typos, but main points are:

  • colspan for proper formatting
  • nested loops (inner and outer dict)

(dict should not be used as a variable name, renamed todct.)

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement