Conditionals in contents

I’m working on my first hugo-driven site. I really like working with it.

My first big obstacle is figuring out how to break up a list page with over 200 titles. The nature of this particular beast is such that I’d like it to remain one page, but with anchors throughout the list based on the first letter of each title. Here’s the pseudo logic:

$currentletter = ''
while list
  $firstletter = <<first letter of title>>
  if $currentletter != $firstletter
    <<add back-to-top link>>
    <<build letter-specific anchor>>
    $currentletter = $firstletter
  <<do template-y things for this page>>

I can’t seem to get the hugo-tempate versions of $currentletter and $firstletter to keep in scope during the list iteration.

Use the search. Search for scope and scratch.

Yep. Scratch was what I needed. The solution ended up looking something like this.

{{ $.Scratch.Set "cur" "Z" }}
{{ range .Data.Pages }}
  {{ $currentltr := $.Scratch.Get "cur" }}
  {{ $firstltr := substr .Title 0 1 }}
  {{ if ne $currentltr $firstltr }}
    <p id="letter{{ $firstltr }}"><a href="#top">Back to top</span></a></p>
    {{ $.Scratch.Set "cur" $firstltr }}
  {{ end }}
  <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ end }}