Excluding current page when using 'shuffle`

See the code for context. When shuffling some items, sometimes the current post appears in the list. Should that happen?

{{- range  site.RegularPages | shuffle | first 8 }}
  {{- if not (eq $.RelPermalink .RelPermalink) }}
     <a href="{{ .RelPermalink }}">{{ .Title }}</a>
  {{- end }}
{{- end }}

Not answering your main meta question, but in this case I would recommend you do:

{{ if ne $ . }}
1 Like

This is a comment through a black box, because everything depends on the full layout file, not just 5 lines of code. The $.RelPermalink is, what might be the problem. The $ might not be the current page, but one that is within the current page (list pages, tag-lists, etc).

Also, you range with $k, $v, but in the range you use the . - this shouldn’t be an issue, but could be.

This is a bit cleaner, and you will always list 8 pages.

In your original code you would list either 7 or 8, depending on whether the current page is in the first 8 after shuffling.

layouts/_default/single.html
{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}

  {{ range site.RegularPages | complement (slice .) | shuffle | first 8 }}
    <a href="{{ .RelPermalink }}">{{ .Title }}</a>
  {{ end }}

{{ end }}
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.