# Scoping a variable outside of and if statement (or two)

**URL:** <https://discourse.gohugo.io/t/scoping-a-variable-outside-of-and-if-statement-or-two/13229>\
**Category:** support\
**Created:** [July 29, 2018, 10:20pm UTC](https://discourse.gohugo.io/t/scoping-a-variable-outside-of-and-if-statement-or-two/13229 "2018-07-29T22:20:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)\
**Post date:** [July 29, 2018, 10:20pm UTC](https://discourse.gohugo.io/t/scoping-a-variable-outside-of-and-if-statement-or-two/13229/1 "2018-07-29T22:20:47Z")

</div>

Given the following:

```auto
{{ $btotal := $anumber }}
{{ $gtotal := $btotal }}
{{ if isset .Params "other" }}
    {{ if eq .Params.other true }}
        {{ $ntotal := $anothernumber }}
        {{ $gtotal := math.Round (div (add $btotal $ntotal) 2) }}
    {{ end }}
{{ end }}
{{ $gtotal }}

```

And assuming all page parameters are present and set to true…

This produces the first value of $gtotal and ignores the new value set in the if statements. I’m sure the explanation is much simpler than my question. Is this about setting the scope of the variable?

Basically what I’m wanting to do is set the value of a variable, but then if another parameter is present, redefine that first variable with some math operations.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 29, 2018, 10:22pm UTC](https://discourse.gohugo.io/t/scoping-a-variable-outside-of-and-if-statement-or-two/13229/2 "2018-07-29T22:22:33Z")

</div>

> [@Hugo + Go 1.11 hype thread](https://discourse.gohugo.io/t/hugo-go-1-11-hype-thread/13198/6):
>
> No, it would be =.

Until then, Scratch is your tool

---

<div class="post-metadata">

**Author:** ![bradbice](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bradbice/32/14162_2.png) [@bradbice](https://discourse.gohugo.io/u/bradbice)\
**Post date:** [July 29, 2018, 10:42pm UTC](https://discourse.gohugo.io/t/scoping-a-variable-outside-of-and-if-statement-or-two/13229/3 "2018-07-29T22:42:55Z")

</div>

Thank you, @bep!

Here is my new code, for any further searches by anyone:

```auto
{{ $btotal := $anumber }}
{{ .Scratch.Set "gtotal" $btotal }}
{{ if isset .Params "other" }}
    {{ if eq .Params.other true }}
        {{ $ntotal := $anothernumber }}
        {{ .Scratch.Set "gtotal" (math.Round (div (add $btotal $ntotal) 2)) }}
    {{ end }}
{{ end }}
{{ .Scratch.Get "gtotal" }}

```
