Range index stopped working somewhere between 0.46 and 0.49.2

I have the following code snippet in my templates:

{{ range $index, .Paginator.Pages }}
<article class="{{ if eq $index 0 }}first{{ end }}">
... more ...
{{ end }}

This throws the following error:

ERROR 2018/10/27 14:11:45 Error while rendering "section" in "": template: _default/list.html:3:34: executing "main" at <.Paginator.Pages>: undefined variable: $elem_index

Did anything change between those Hugo versions (0.47 some months ago worked)? I assume this way of indexing was wrong before but did not hassle Hugo, until now :frowning:

edit: Using Hugo extended now.

The " $elem_index" suggests you are not showing the full source of that template, which makes it impossibe for us to guess.

I tried to rename it and copied half wrong piece of code, sorry.

I solved it by adding a parameter before .Paginator.Pages:

working:

{{ range $elem_index, $value := .Paginator.Pages }}

not working (not throwing errors before):

{{ range $elem_index, .Paginator.Pages }}

and for “posterities sake” the whole code:
the errorr is thrown in the third line.

{{ define "main" }}

  {{ range $elem_index, $value := .Paginator.Pages }}
  <article class="{{ if eq $elem_index 0 }}first{{ end }}">
    {{ .Scratch.Set "firstitem" "" }}
    <header class="title">
      <h2 class="entry-title">
        <a href="{{ .Permalink }}">{{ .Title }}</a>
      </h2>
      <div class="meta">
        <div class="row">
          <div class="col-md-9 is--taglist">
            {{ with .Params.tags }}
              <i class="fas fa-tags" aria-hidden="true"></i>&nbsp;
              {{ range $i, $e := . }}
                {{ if $i }}/{{ end }}
                <a href="{{ "tags/" | absURL }}{{ $e | urlize }}" rel="tag">{{ $e }}</a>
              {{ end }}
            {{ end }}
          </div>
          <span class="col-md-3 text-sm-left text-xs-left text-md-right">
            <time class="post-date updated" datetime="{{ .Date.Format "2006-01-02T15:04:05-0700" }}">
              <i class="fas fa-calendar-alt" aria-hidden="true"></i>&nbsp;
              {{ .Date.Format "2" }}.
              {{ index $.Site.Data.months.months .Date.Month }}
              {{ .Date.Format "2006" }}
            </time>
          </span>
        </div>
      </div>
    </header>
    <div class="text entry-content">
      {{ with .Params.featured_image }}
        <img src="{{ . | absURL }}" />
      {{ end }}
      {{ .Content }}
    </div>
    <footer>
      <div class="meta">
        <div class="row">

          <div class="col-md-6">
          <i class="fas fa-pencil-alt" aria-hidden="true"></i>
          Von <address class="vcard author post-author"> <span
          class="fn"> <a href="https://kollitsch.de" title="Website von Patrick Kollitsch
          besuchen" rel="author external">Patrick Kollitsch</a> </span>
           </address>
          </div>
          <div class="col-md-6 text-sm-left text-xs-left text-md-right">
          {{ with .Params.showcomments }}
            <i class="fas fa-comments" aria-hidden="true"></i>
            <a href="https://samui-samui.de/2017/10/entscheidung-thai-style/#disqus_thread"
          data-disqus-identifier="4855 https://samui-samui.de/?p=4855">0 Kommentare</a>
          {{ end }}
          </div>

        </div>
      </div>
    </footer>
  </article>
  {{ end }}

  {{ partial "pagination/posts.html" . }}

{{ end }}

I’m pretty sure the above has never worked. There was a slightly related improvement in Go 1.11 (variable reassignment) that we used to build 0.48 (I think), but that seems odd … But I’m curious enough so I will check it …

Yeah, I think it falls into the category “It was wrong but it worked until we really looked at it” :slight_smile: