I have begun to move from models, modules, and controllers into actually 
writing the view for my application. Now, I am at the point where I need to 
loop through a Rows object, building a TR for each row. I wrote a version 
that works by writing it in HTML, using {{=x}} where needed, but it looks 
all cluttered and difficult to read. This is the original code that works:

{{extend 'layout.html'}}
<h1>Time Clock for {{=user.first_name}} {{=user.last_name}}</h1>
{{if len(rows) > 0:}}
    {{
        def get_day_of_year(the_date):
            return the_date.timetuple().tm_yday
            
        def full_date_name(the_date):
            return the_date.strftime('%A, %B %d, %Y')
            
        def time_string(the_date):
            return the_date.strftime('%I:%M %p')
            
        prev_day_of_year = None
        table_open = False
        for row in rows:
            if get_day_of_year(row.calculated_start) != prev_day_of_year:}}
                {{if table_open:}}
                    </tbody></table>
                {{pass}}
                <h4>{{=full_date_name(row.calculated_start)}}</h4>
                <table><thead><tr><th>Time in:</th><th>Time 
out:</th></tr></thead><tbody>
               
 
<tr><td>{{=time_string(row.calculated_start)}}</td><td>{{=time_string(row.calculated_end)}}</td></tr>
                {{table_open = True}}
                {{prev_day_of_year = get_day_of_year(row.calculated_start)}}
            {{else:}}
               
 
<tr><td>{{=time_string(row.calculated_start)}}</td><td>{{=time_string(row.calculated_end)}}</td></tr>
            {{pass}}
        {{pass}}
        </tbody></table>
    <h5>Total hours worked this week: {{=hours_worked}}</h5>
{{else:}}
    <h5>No time clock entries for this week.</h5>
{{pass}}

I know that web2py has HTML helpers like TABLE(), TR(), etc, but how would I 
build a table in this way, while in a for loop? This is what I tried so far, 
but it fails so horribly I don't even know where the error is:

{{extend 'layout.html'}}
<h1>Time Clock for {{=user.first_name}} {{=user.last_name}}</h1>
{{if len(rows) > 0:}}
    {{
        def get_day_of_year(the_date):
            return the_date.timetuple().tm_yday
            
        def full_date_name(the_date):
            return the_date.strftime('%A, %B %d, %Y')
            
        def time_string(the_date):
            return the_date.strftime('%I:%M %p')
            
        prev_day_of_year = None
        tables = []
        trs = None
        for row in rows:
            if get_day_of_year(row.calculated_start) != prev_day_of_year:
                if trs is not None:
                    tables[] = (H4(full_date_name(row.calculated_start)), 
trs)
                    trs = []
                    
                trs[] = TR((TD(time_string(row.calculated_start)), 
TD(time_string(row.calculated_end))))
                prev_day_of_year = get_day_of_year(row.calculated_start)
            else:
                trs[] = TR((TD(time_string(row.calculated_start)), 
TD(time_string(row.calculated_end))))
                
        for table in tables:
            response.write(table[0])
            response.write(TABLE(
                (THEAD(
                    TR(
                        (TH('Time in:), TH('Time out'))
                    )
                ),
                TBODY(
                    
                ))
            ))
    }}
    <h5>Total hours worked this week: {{=hours_worked}}</h5>
{{else:}}
    <h5>No time clock entries for this week.</h5>
{{pass}}

Reply via email to