Reputation: 478
I am trying to solve this web2py
problem and I think I have some basic misunderstanding of what's going on.
Say I have a shop, and I want to have in my index.html
many boxes with products. I want each such box to be parsed from a template.
What I tried to do to have the following architecture. In the controller I have
def index():
products = db().select(db.products.ALL)
return dict(products=products)
in index.html
:
{{for i in range(0,len(products)):}}
{{ context=dict(product=products[i]) }}
{{ =response.render('template.html', context)}}
{{pass}}
and in template.html
I have something like
<div id=...> <h1> {{=product.price}} </h1>...
The problem is that the result is read literaly. That is, when browse the index.html
I see html tags:
I suspect that my whole approach is wrong. How should one do that?
Upvotes: 1
Views: 1066
Reputation: 25536
{{for product in products:}}
{{=XML(response.render('template.html', product.as_dict()))}}
{{pass}}
In template.html:
<div id=...> <h1> {{=price}} </h1>...
response.render()
returns a string, and all strings are escaped in templates. To prevent the escaping, you must wrap the string in XML()
.
The above also includes a couple of simplifications. In the for
loop, you can just iterate directly over the rows in products
, and you can then convert a single product row to a dict and pass that dict as the context to template.html (so you can just refer to price
rather than product.price
).
Note, if you don't need to use template.html elsewhere, you might as well just move its contents directly into the for
loop within index.html.
Upvotes: 3