Hugo

Tables and math, from nested front matter

I’ve done the easy parts, I’ve got nested front matter, depicting line items in an invoice:

fee:
- description: Monthly Hosting
  cost: 50
  quantity: 3
- description: Tech Support
  cost: 75
  quantity: 6
- description: Monthly Hosting
  cost: 20
  quantity: 6

I can output my table just fine:

<table>
    <thead>
        <tr>
            <th>Description</th>
            <th>Cost</th>
            <th>Qty</th>
            <th>Line Total</th>
        </tr>
    </thead>
    <tbody>
    {{ range .Params.fee }}
        <tr>
            <td>{{ .description }}</td>
            <td>{{ .cost }}</td>
            <td>{{ .quantity }}</td>
            <td>{{ mul .cost .quantity }}</td>
        </tr>
    {{ end }}
    </tbody>
</table>

Produces expected table:

<table>
    <thead>
        <tr>
            <th>Description</th>
            <th>Cost</th>
            <th>Qty</th>
            <th>Line Total</th>
        </tr>
    </thead>
    <tbody>
    
        <tr>
            <td>Monthly Hosting</td>
            <td>50</td>
            <td>3</td>
            <td>150</td>
        </tr>
    
        <tr>
            <td>Tech Support</td>
            <td>75</td>
            <td>6</td>
            <td>450</td>
        </tr>
    
        <tr>
            <td>Monthly Hosting</td>
            <td>20</td>
            <td>6</td>
            <td>120</td>
        </tr>
    
    </tbody>
</table>

Okay, I want to total the line totals. I’m not sure where to go from here. :slight_smile:

Does scratch help?

1 Like

Written from my phone so please forgive terseness.

I would get your total like so:

{{ $total := 0 }}
{{ range .Params.fee }}
  {{ $line_total := mul .cost .quantity }}
  {{ $total = add $total $line_total }}
{{ end }}
2 Likes

This does not fit exactly to your setup.

I needed some kind of internationalisation and take the data from /data/ folder (hoping for this to come):

<td class="price">{{ lang.NumFmt 2 $item.price_per_item "- , ." }} €</td>
<td class="sum">{{ $sum := mul $item.quantity $item.price_per_item }}{{ lang.NumFmt 2 $sum "- , ." }} €</td>

This can probably be simplified in some way.

Thanks Zachary! That’s what I needed.

I still need to look up the notation to get the table cells lined up as expected, but here’s the code and output:

<table>
    <thead>
        <tr>
            <th>Description</th>
            <th>Cost</th>
            <th>Qty</th>
            <th>Line Total</th>
        </tr>
    </thead>
    <tbody>
    {{ $total := 0 }}
    {{ range .Params.fee }}
        {{ $line_total := mul .cost .quantity }}
        <tr>
            <td>{{ .description }}</td>
            <td>{{ .cost }}</td>
            <td>{{ .quantity }}</td>
            <td>{{ $line_total }}</td>
        </tr>
        {{ $total = add $total $line_total }}
    {{ end }}
        <tr>
            <td>
              {{ $total }}
            </td>
         </tr>
    </tbody>
</table>
<table>
    <thead>
        <tr>
            <th>Description</th>
            <th>Cost</th>
            <th>Qty</th>
            <th>Line Total</th>
        </tr>
    </thead>
    <tbody>
    
    
        
        <tr>
            <td>Monthly Hosting</td>
            <td>50</td>
            <td>3</td>
            <td>150</td>
        </tr>
        
    
        
        <tr>
            <td>Tech Support</td>
            <td>75</td>
            <td>6</td>
            <td>450</td>
        </tr>
        
    
        
        <tr>
            <td>Monthly Hosting</td>
            <td>20</td>
            <td>6</td>
            <td>120</td>
        </tr>
        
    
        <tr>
            <td>
              720
            </td>
         </tr>
    </tbody>
</table>

Thanks for the pointer. I’m not sure if I’ve dodged a bullet or not; one of these days I’m sure I’ll need to learn scratch. :slight_smile:

1 Like

Happy to help @maiki

1 Like