When you invoke the view, you pass a structure with it contains the data to display. The trick is to design your structure to contain all the data that the views needs. In your case that might be a structure containing a total and a slice containing numbers. In the view you iterate through the slice displaying each number, and then display the total. If you want several columns then you might pass a structure containing a total and a slice of structures, where each structure contains the values for one row.
Another typical example of this kind of thing is a page that contains some validated data with error messages. In that case, your structure would contain the data and the error messages.
type Row struct {URL stringHits intTimeSum time.Duration}
{% func TableWithTotals(rows []Row) %}{% code// declare ordinary Go variables inside template for calculating the 'totals' row.var (hitsSum inttimeSum time.Duration)// Arbitrary data transformations may be performed here via usual Go code.// For instance, rows may be sorted, filtered, aggregated and sliced before outputting// them in the template.%}<table><tr><th>#</th><th>URL</th><th>Hits</th><th>Avg request duration</th></tr>{% for i, r := range rows %}{% code// increment totalshitsSum += r.HitstimeSum += r.TimeSum%}<tr><td>{%d i %}</td><td>{%s r.URL %}</td><td>{%d r.Hits %}</td><td>{%v r.TimeSum / time.Duration(r.Hits) %}</td></tr>{% endfor %}<tr><th>Totals</th><th></th><th>{%d hitsSum %}</th><th>{%v timeSum / time.Duration(hitsSum) %}</th></tr></table>{% endfunc %}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.