Eel icon indicating copy to clipboard operation
Eel copied to clipboard

How to pass argument to loop via jinja

Open evheniu opened this issue 4 years ago • 4 comments

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>

evheniu avatar May 19 '21 19:05 evheniu

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.

eddyizm avatar Jan 29 '22 15:01 eddyizm

Any resolution to this problem?

Dan-Adamson avatar May 16 '22 12:05 Dan-Adamson

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?

martin-foka avatar Sep 18 '22 00:09 martin-foka

@maRT-sk That is an outstanding work around. Seems like a good fit as I have heard alpine being very lightweight and minimal.

eddyizm avatar Sep 18 '22 01:09 eddyizm