I don't understand this `counter`. Can you please use `loop.index` (maybe in the outer one to set a variable?) and use that?
[jinja2 about loop controls](http://jinja.pocoo.org/docs/templates/#loop-controls)