How to add up the total params value from the section

Hi all

How to get the total value from the specific params. Let say I have a param called fund in my project section/content,

---
fund: 7000 #value that i need to calculate
---

what I need is to sum/calculate that from every project I made in my home page for example <span>total fund<span>....

with my limitation I try to use len and add

{{ $fund := .Params.raised_fund }}
{{ $fundCount := len $fund }}

#the result is empty
{{ $fund := .Params.raised_fund }}
{{ $fundCount := add $fund }}

#the result is error
ERROR 2018/09/01 14:17:40 Error while rendering "home" in "": template: index.html:11:7: executing "main" at <partial "singles/hom...>: error calling partial: template: partials/singles/homepage.html:184:17: executing "partials/singles/homepage.html" at <add>: wrong number of args for add: want 2 got 1
Total in 103 ms
ERROR 2018/09/01 14:17:40 Failed to rebuild site: logged 1 error(s)

thank you

I haven’t tried any of this, so this is just me spitballing some ideas.

First of all please make sure you are using the latest version, so you can actually overwrite variables. (https://gohugo.io/news/0.48-relnotes/).

You would also have to initialize the variable somewhere:

{{ $fundCount := 0 }}
{{ $fund := .Params.raised_fund }}
{{ $fundCount = add $fundCount $fund }}

If you don’t use the latest version, you need to use the scratch workaround.

Also make sure that you get the original count as well, since add needs 2 parameters.

1 Like

Hi
I’m using latest version Hugo :slight_smile:

Right now I have the following code in my homepage (index.html)

{{ range (where .Data.Pages "Type" "project") }} 
          {{ $fundCount := 0 }}
         {{ $fund := .Params.raised_fund }}
        <span>Total Fund:</span>{{ $fundCount = add $fundCount $fund }}
{{ end }}

the result is empty. any other suggestion?

If I understand correctly you currently redefine $fundCount every time as 0 when you go through the pages.

Maybe try something like that:

{{ $fundCount := 0 }}
{{ range (where .Data.Pages "Type" "project") }}
    {{ $fund := .Params.raised_fund }}
    {{ $fundCount = add $fundCount $fund }}
{{ end }}
<span>Total Fund:</span> {{ $fundCount }}

And please explicitly check that you are using 0.48 by typing hugo version, since some packages are not yet updated.

1 Like

Here you go. Tested and working.

{{ $var := .Scratch }}
{{ $posts := (where .Site.RegularPages ".Params.fund" "ne" nil) }}
{{ $fund := 0 }}
{{ range $posts }} 
{{ $fund := .Params.fund | int }}
{{ $var.Add "fund" $fund }}
{{ end }}
{{ $sum := $var.Get "fund" }}
Total: {{ add $fund $sum }}

Basically first store .Scratch in a variable
Then store in another variable the pages that have said parameter.
Then define the $fund variable to 0
Range through above set of pages.
Store the parameter in a variable and convert the string to a number using int
Store the above to a variable with .Scratch
Then outside the above context do the math.

PS. I doubt that the above can be done just with the variable overwrites introduced in Go 1.1. But please prove me wrong.


EDIT

Turns out you can do the above with the variable overwrites introduced in Go 1.11

@olikami your snippet needed int to convert the stings to numbers

Here is the working snippet (modified for my test project):

{{ $fundCount := 0 }}
{{ range (where .Site.RegularPages ".Params.fund" "ne" nil) }}
    {{ $fund := .Params.fund | int }}
    {{ $fundCount = add $fundCount $fund }}
{{ end }}
<span>Total Fund:</span> {{ $fundCount }}

1 Like

Awesome works like a charm :+1:

Yes silly me redefine that as 0 :sweat_smile: Thank you for the explanation, now I’m little bit understand about add function and variable overwrite

This one is now just outta interest, but why do you need to pipe to int? I thought if there aren’t any quotation marks it should automagically be seen as an int according to the toml-specs.

1 Like

thank you, this can be an alternative in case anyone here who have the same experience but not used the latest version of Hugo.

test the code and the result is

 error calling partial: template: partials/singles/homepage.html:190:10: executing "partials/singles/homepage.html" at <add $fund $sum>: error calling add: Can't apply the operator to the values

any idea?

How if I already have the value in number? should I put the int

1 Like

In that case you do not need int.

@olikami You are right. I had it in quotation marks.

1 Like

UPDATE:

Just want to confirm that Your code is also working

Thank you guys