Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template for and while cycles #31

Open
lucasub opened this issue Dec 26, 2022 · 3 comments
Open

Template for and while cycles #31

lucasub opened this issue Dec 26, 2022 · 3 comments

Comments

@lucasub
Copy link

lucasub commented Dec 26, 2022

Hello, is possible make "for" and "while" cycles inside html template?
How can I do this?
Thank you
Luca

@lixas
Copy link

lixas commented Dec 27, 2022

I had the same request and was able to solve it here #28

@artemisoftnian
Copy link

I was looking how to make a loop work inside the html template, I just read about inline python syntax. but the end code its a little bit difficult to even look at it!. So was I end doing was, passing the data variable to a javascript variable using: for example:
var MyJsVar = {{ str(PhewData) }}
and then use that variable to do the job done!

@Nikorasu
Copy link

Nikorasu commented Apr 12, 2023

Here's an example how I created an html table, from a list, using a for-loop within double {{}} in the html file that gets loaded via render_template():
{{''.join([f'<tr> <td>{entry[0]}</td> <td>{entry[1]}</td> </tr>' for entry in entries])}}
Basically uses list comprehension, to make a list of strings containing the html for each table row, and also uses f-string to insert the individual sub-entries in my list (cause it was a list of names and emails in my case) into the string. Then uses ''.join() to make that new list into one large string, which the double-brackets then return as html code when run through the render_template.
When I return the render_template in my main python code, I pass the entries list to it:
return render_template("tablepage.html", entries=myTableList)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants