Basic variable operations in templates

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

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

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 :=

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.

Thanks for your reply!

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 :slight_smile: But all of that power isn’t exposed into the templates. Hence the many Hugo custom template functions.