I am creating a web store with HTML and Bottle (Python). I know how to pass data between HTML and Python, as well as how to write Python code inside HTML (reference: http://karrigell.sourceforge.net/en/pythoninsidehtml.html).
What I struggle with is writing Python code inside the reference that gets passed from HTML to Python.
I load images from a database since I have a lot of them. I set and pass the variables image_name(index) to HTML like this:
@get('/store')
def showStore():
images = models.images() # reads from database
data = ', number_of_images={}'.format(len(images))
for (id, title, path) in images:
data += ', image_name{}={}, '.format(id, path)
data += 'image_title{}={}'.format(id, title)
return template('store.html' + data)
The relevant part of store.html:
<!-- Portfolio Gallery Grid -->
<div class="row">
<% for i in range(1, number_of_images+1): %>
<div class="column">
<div class="content">
<img src="static/images/{{image_name<%i%>}}.jpg">
<h3> {{image_title<%i%>}} </h3>
</div>
</div>
<% end %>
</div>
Since Bottle is not that well known: {{image_title}} in HTML gets the value passed in from the template (html, variables). Block <% %> enables you to write Python code inside it.
Everything worked properly when I didn't use indexing of image properties inside the HTML, so HTML and the for loop do work, the problem is with this part:
{{image_title<%i%>}}
<%i%> should in my opinion create a variable image_titlei but apparently it doesn't.
I get a template not found error, probably because the variables I am passing in (from the .py file) do not exist in the HTML.
The reason I would very much like to do this dynamically instead of hardcoding every image is that I have a lot of those images and every image has a whole table of relevant data I left out here because I would retrieve it in the same way as image's name and title.
I would appreciate any help with proper combining Python inside HTML with variables passed in since I really want to do it dynamically