On Friday, April 7, 2017 at 9:48:37 AM UTC-4, Brian M wrote:
>
> Anthony, this is what I was looking for. Thanks! I am trying not to build
> the html myself and use the helpers instead. I am going for readability and
> ease of use for others to modify later. In all the examples in the book
> nowhere did it show the append and insert for the HTML helpers I looked at.
>
> The gist of it was that I needed to was to not convert everything to
> strings, but make them the various helper objects so when passed to the
> TBODY and TABLE they don't add <tr><td>
>
>
> tbody=TBODY()
> headerrow=TR()
>
>
> #######################################################
> #big fancy query loop and processing to accomplish the following#
> headerrow.append('column 1')
> tbody.append(TR(TD('db data processed and gussied up')))
> #######################################################
>
>
> thead=THEAD(headerrow)
> table=TABLE(_class='pure-table pure-table-bordered')
> table.append(thead)
> table.append(tbody)
> htmltable=XML(table)
>
>
Note, some of this is covered here:
http://web2py.com/books/default/chapter/29/05/the-views#HTML-helpers
You can do as you have above and directly .append() to a helper object such
as a TBODY. But you can also simply build a Python list of helpers and then
pass the entire list to the parent. For example:
table_rows = [TR(TD(record.name)) for record in records]
table_body = TBODY(*table_rows)
The above uses *table_rows argument unpacking, but you can in fact simply
pass a list to TBODY rather than using the unpacking syntax:
table_body = TBODY(table_rows)
Now let's say you have some complicated code to produce a row of the table.
You can abstract that into a separate function and then build the whole
table body in a simple one-liner:
def build_complicated_row(record):
cells = []
cells.append(TD(some_function(record)))
... # more code to add TDs
return TR(cells, _class='some_class')
table_body = TBODY([build_complicated_row(record) for record in records])
Anthony
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.