sponsfreixes / jinja2-fragments

Render Jinja2 template block as HTML page fragments on Python web frameworks.
MIT License
253 stars 14 forks source link

'loop' is undefined #29

Open dAnjou opened 6 months ago

dAnjou commented 6 months ago

Hi,

I'm migrating a traditional Flask+Jinja2 app to one with HTMX. Similar to #23, I'm using a block inside a loop.

I'm also using the special loop.index variable, which is when I'm getting: jinja2.exceptions.UndefinedError: 'loop' is undefined.

I know I can get around that by simply wrapping my iterable into list(enumerate(...)), but it'd probably make a smoother experience if loop was available.

Not sure if that's even possible. If not, it should probably be documented as a caveat.

🙏

sponsfreixes commented 6 months ago

Good point about at least adding that to the docs!

gconklin commented 4 months ago

My hacky workaround for this was something like:

class FakeLoop:
    last = True
return render_block(file, fragment, loop=FakeLoop())

but I didn't have anything complex and only needed that one attribute.