# Basic variable operations in templates

**URL:** <https://discourse.gohugo.io/t/basic-variable-operations-in-templates/1870>\
**Category:** feature\
**Created:** [October 12, 2015, 2:22pm UTC](https://discourse.gohugo.io/t/basic-variable-operations-in-templates/1870 "2015-10-12T14:22:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raistleitner](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/raistleitner/32/770_2.png) [@raistleitner](https://discourse.gohugo.io/u/raistleitner)\
**Post date:** [October 12, 2015, 2:22pm UTC](https://discourse.gohugo.io/t/basic-variable-operations-in-templates/1870/1 "2015-10-12T14:22:06Z")

</div>

I’m kind of new to Hugo and like it so far, now I’m into some more advanced topics.

I want to show the next three pages of a section at the end of a page, and if they do not exist yet (e.g. at the newest article) the three older ones should be shown. This leads to serious troubles in solving this efficiently, since there is no way of building and assigning arrays to custom variables depending on some if statements.

Am I missing something or are there really no basic operations for lists/arrays available? I really would like to modify lists and use them in a range command accordingly.

I appreciate your help,  
Robert

---

<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:** [October 12, 2015, 3:35pm UTC](https://discourse.gohugo.io/t/basic-variable-operations-in-templates/1870/2 "2015-10-12T15:35:43Z")

</div>

> [@raistleitner](#):
>
> Am I missing something or are there really no basic operations for lists/arrays available? I really would like to modify lists and use them in a range command accordingly.

The built-ins in Go templates won’t help you here. This addon to Hugo 0.15-DEV might:

> <https://github.com/gohugoio/hugo/pull/1311/files>
>
> Hello,
> go newbie here, so be kind and let me know if I'm doing something stupid …there :)
> 
> I'll start by what I'm trying to solve. I'm using shortcodes to add references inline and automatically generate list of references at the end of the post. With current scratch, I was able to collect them and list them in the same order as they were mentioned. But I want them sorted and de-duplicated. And I don't think that's possible right now, so this is my solution.
> 
> I think something like that is relatively common use case and maybe it might be useful for other things.

---

<div class="post-metadata">

**Author:** ![raistleitner](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/raistleitner/32/770_2.png) [@raistleitner](https://discourse.gohugo.io/u/raistleitner)\
**Post date:** [October 13, 2015, 6:51am UTC](https://discourse.gohugo.io/t/basic-variable-operations-in-templates/1870/3 "2015-10-13T06:51:24Z")

</div>

Thanks, this can be useful when stable!

Nevertheless I want to show you the (messy) solution I came up with, just for reference…

```
    {{ if .NextInSection }}
      {{ $.Scratch.Set "cur" .NextInSection }}
      {{ $.Scratch.Set "usenext" true }}
    {{ else }}
      {{ if .PrevInSection }}
        {{ $.Scratch.Set "cur" .PrevInSection }}
        {{ $.Scratch.Set "usenext" false }}
      {{ end }}
    {{ end }}

    <div class="col-md-4">
      {{ with $.Scratch.Get "cur" }}
      {{ partial "interview-preview.html" . }}
      {{ end }}
    </div>

    {{ if eq ($.Scratch.Get "usenext") true }}
      {{ if ($.Scratch.Get "cur").NextInSection }}
        {{ $.Scratch.Set "cur" ($.Scratch.Get "cur").NextInSection }}
      {{ else }}
        {{ $.Scratch.Set "cur" .PrevInSection }}
        {{ $.Scratch.Set "usenext" false }}
      {{ end }}
    {{ else }}
      {{ $.Scratch.Set "cur" ($.Scratch.Get "cur").PrevInSection }}
    {{ end }}

    <div class="col-md-4">
      {{ with $.Scratch.Get "cur" }}
      {{ partial "interview-preview.html" . }}
      {{ end }}
    </div>

    {{ if eq ($.Scratch.Get "usenext") true }}
      {{ if ($.Scratch.Get "cur").NextInSection }}
        {{ $.Scratch.Set "cur" ($.Scratch.Get "cur").NextInSection }}
      {{ else }}
        {{ $.Scratch.Set "cur" .PrevInSection }}
        {{ $.Scratch.Set "usenext" false }}
      {{ end }}
    {{ else }}
      {{ $.Scratch.Set "cur" ($.Scratch.Get "cur").PrevInSection }}
    {{ end }}

    <div class="col-md-4">
      {{ with $.Scratch.Get "cur" }}
      {{ partial "interview-preview.html" . }}
      {{ end }}
    </div>
  {{ end }}

```

I don’t really get why I have to use Scratch anyways - it would be much more comfortable to do operations on “normal” variables and assign them with :=

---

<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:** [October 13, 2015, 6:57am UTC](https://discourse.gohugo.io/t/basic-variable-operations-in-templates/1870/4 "2015-10-13T06:57:05Z")

</div>

> [@raistleitner](#):
>
> I don’t really get why I have to use Scratch anyways - it would be much more comfortable to do operations on “normal” variables and assign them with :=

You would have to tell that to Rob Pike in Google. You can do simple variable assignment, but you have all kinds of scoping issues.

---

<div class="post-metadata">

**Author:** ![raistleitner](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/raistleitner/32/770_2.png) [@raistleitner](https://discourse.gohugo.io/u/raistleitner)\
**Post date:** [October 16, 2015, 8:57am UTC](https://discourse.gohugo.io/t/basic-variable-operations-in-templates/1870/5 "2015-10-16T08:57:14Z")

</div>

Thanks for your reply!

> [@bep](#):
>
> You would have to tell that to Rob Pike in Google. You can do simple variable assignment, but you have all kinds of scoping issues.

Yes I experienced that already… Maybe I should have a look into Go to get an idea why things are this way in Hugo…

---

<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:** [October 16, 2015, 9:09am UTC](https://discourse.gohugo.io/t/basic-variable-operations-in-templates/1870/6 "2015-10-16T09:09:05Z")

</div>

> [@raistleitner](#):
>
> Yes I experienced that already… Maybe I should have a look into Go to get an idea why things are this way in Hugo…

You would have to look into the Go (text, html) templates – Go as a language has of course full support for “basic variable operations” and then some 🙂 But all of that power isn’t exposed into the templates. Hence the many Hugo custom template functions.
