How to pass argument to loop via jinja
Describe the problem How do I pass argument b to a for loop to jinja template?
Code snippet(s) Example:
import eel
eel.init("web")
b = [1,2,3]
eel.start("templates/main.html",size =(350, 400), jinja_templates='templates')
<html>
<label>
<div>Example:</div>
<select name="a">
{% for a in b %}
<option value="{{ a }}">{{ a }}</option>
{% endfor %}
</select>
</label>
</html>
Did you figure this out? This seems essential to use jinja with it but I'm suspecting it is not possible since I don't seen any examples of passing context.
Any resolution to this problem?
I use Alpine.js as a workaround for my Jinja templates. All you need for your loop are alpine attributes:
import eel
@eel.expose
def b():
return ["1", "2", "3"]
@eel.expose
def header():
return "I am header"
eel.init('web')
eel.start('templates/base.html', jinja_templates='templates')
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
<script type="text/javascript" src="/eel.js"></script>
<script src="//unpkg.com/alpinejs" defer></script>
</head>
<body x-data>
<h1 x-text="eel.header()"></h1>
<template x-for="a in eel.b()">
<p x-text="a"></p>
</template>
</body>
</html>
Alpine attributes can also be used in Eel apps for: getting variables, modifying python variables, html if statements, showing/hiding DOM elements, event listeners, transitions... Is there a better solution to pass variables to HTML?
@maRT-sk That is an outstanding work around. Seems like a good fit as I have heard alpine being very lightweight and minimal.