That was a very helpful example. Thank you. So using the web2py TABLE helpers
is best used for creating simple, static tables where the rows aren't
generated? I guess that's ok, I always thought that the controller was meant to
get the data, while the views format the data. But if the web2py way is to
format in the controller and make the views contain as little Python code as
possible, then that's they way I'll write it.
On Mar 3, 2011, at 11:36 AM, pbreit wrote:
> I usually try to put less logic in my views. For one, I try to assemble all
> the data fields ahead of time either in the controller or with virtual fields.
>
> Then in the view, I try to just end up looping through the dict. I normally
> use HTML tags but using Web2py tag helpers should be fine as well.
>
> See example below. Notice that I have a sliver of logic in there to display
> "no results" if there are no items. Also note that I have a couple fields
> like "item.last_date" that I have generated as a virtual field. In this way,
> you keep your views looking mostly like regular pages of HTML with some logic
> here and there.
>
> <table width="100%">
> <thead>
> <tr>
> <th></th>
> <th class="ttl">Description</th>
> <th class="prc">Current Price</th>
> <th class="prc">Final Price</th>
> </tr>
> </thead>
> <tbody>
> {{if len(items)==0:}}
> <tr><td colspan=4 align=middle><br>No items</td></tr>
> {{else:}}
> {{for item in items:}}
> <tr>
> <td>
> {{if len(item.image)==0:}}
> {{=A(IMG(_src=URL('static','images/no-photo.png'),
> _width='80px'), _href=URL(c='default', f='item', extension='', args=item.id,
> ))}}
> {{else:}}
> {{=A(IMG(_src=URL(c='default', f='download',
> args=item.image), _width='100px'), _href=URL(c='default', f='item',
> extension='', args=item.id))}}
> {{pass}}
> </td>
> <td class="ttl">{{=A(item.title, _href=URL(c='default',
> f='item', extension='', args=item.id))}}</td>
> <td class="prc"><b>${{=item.current_price}}</b></td>
> <td class="prc">${{=item.last_price}} on
> {{=item.last_date}}</td>
> </tr>
> {{pass}}
> {{pass}}
> </tbody>
> </table>