# How can I calculate a cumulative total?

I have a hierarchy of sections…

/content/logs/section-one
/content/logs/section-one/step-one
/content/logs/section-one/step-two
/content/logs/section-one/step-three
/content/logs/section-two
/content/logs/section-two/step-one
/content/logs/section-two/step-two

Each of the leaf pages have frontmatter that indicates how many hours of work were involved in the log entry…
i.e.
hoursworked: 5.25

I would like each list page in the hierarchy to display the total number of hours worked for all of the leaf pages underneath it, even if multiple levels underneath it.

I’ve successfully created a recursive partial that outputs all the hours worked…

{{ range ( .Pages ) }}
{{ if .IsPage }}
{{ float .Params.hoursworked }}
{{ end }}
{{ if .IsSection }}
{{- partial “recursivepages.html” . -}}
{{ end }}
{{ end }}

But have been unable to figure out how to add all those values together. The add function seems unable to accept the output of a partial, at least the way I’ve implemented it.

Any ideas how I can implement this?

I may have found an answer to my own question…

{{ \$thisPage := .Page }}
{{ \$s := 0.0 }}
{{ range .Site.RegularPages }}
{{ if .IsDescendant \$thisPage }}
{{ \$s = \$s | add .Page.Params.hours }}
{{ end }}
{{ end }}
{{ \$s }} hours of work in this section.

I don’t understand the add syntax of \$s = \$s | add … however… what is with the pipe character?

I stumbled upon append | Hugo where they use the pipe, and thought I’d try it here.

Hello

I don’t understand the add syntax of \$s = \$s | add … however… what is with the pipe character?

The pipe acts for the stream as in Unix.
In this case

\$s = \$s | add .Page.Params.hours

means `I take \$s, towhich I apply something` (this the pipe)
The `something` is `add .Page.Params.hours`
Finally I put the result in `\$s`.

You could have something like `\$s = \$s | <do something> | <do something else>`

Hope this helps.