totalling a column in a html template.

136 views
Skip to first unread message

Carl Ranson

unread,
Aug 25, 2016, 11:14:07 AM8/25/16
to golang-nuts

HI All,

been playing with using go to pull data from a few sources and serve a consolidated view as a webpage.
I've got most of it going ok, but hitting a wall trying to do a totals row in an output table.

anyone know if the html/template package is able to do this?

the fallback is to precalculate the totals and pass them as a struct to the template as well.

thanks,
Carl.


Val

unread,
Aug 25, 2016, 11:32:45 AM8/25/16
to golang-nuts
As you may have guesses, the html/package is not designed to perform such clever calculations. It handles only the html rendering logic.

You have 2 pretty good fallbacks actually : either precalculate and feed values to the template (server-side), or use javascript (client-side).
Cheers
Val

Simon Ritchie

unread,
Aug 26, 2016, 2:55:17 AM8/26/16
to golang-nuts
If you follow the Model View Controller (MVC) model, you should do all of the clever stuff in your controller and just use the view to render the result.

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.

Carl Ranson

unread,
Aug 26, 2016, 6:14:11 AM8/26/16
to golang-nuts

Ok,

Thanks for the answers. I've gone down the route of adding totals to my data structure.

cheers,
CR.

Aliaksandr Valialkin

unread,
Aug 26, 2016, 11:27:23 AM8/26/16
to golang-nuts
Hi Carl,

try quicktemplate instead of html/template. It supports arbitrary data transformations inside the template code, so the 'totals' row may be easily implemented without external code:

Suppose you have the following row struct:

type Row struct {
    URL string
    Hits int
    TimeSum time.Duration
}

Then the template function generating HTML table for the given rows may look like:

{% func TableWithTotals(rows []Row) %}
{% code
    // declare ordinary Go variables inside template for calculating the 'totals' row.
    var (
        hitsSum int
        timeSum 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 totals
          hitsSum += r.Hits
          timeSum += 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 %}

Asit Dhal

unread,
Aug 26, 2016, 11:28:50 AM8/26/16
to Simon Ritchie, golang-nuts
Hi,
If you really need to calculate total, you should leave that completely to the view layer(client side java script).



--
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.

Reply all
Reply to author
Forward
0 new messages