Intersect 2 term lists broken in 0.123.0 onwards

Doing an intersect in a term list page, between that term’s list of pages and another taxonomy’s list pages, is broken since version 0.123.0. The code below, when at layouts/term/list.html, works in version 0.122.0 but doesn’t in version 0.123.x.

(In a category, for example, what it does is showing all tags that are used in the pages with that category.)

{{/* in layouts/term/list.html */}}
{{- $taxo := "tags" -}}
{{- $taxoList := index .Site.Taxonomies $taxo -}}
  {{- $termList := .Data.Pages -}}
  {{- $taxoByTerm := slice -}}
  {{- range $taxoList.ByCount -}}
    {{- if intersect $termList .Pages -}} {{/* before 0.123.0 works, in 0.123.x this intersect always returns an empty list */}}
      {{- $taxoByTerm = $taxoByTerm | append . -}}
    {{- end -}}

    {{- if eq (index .Pages 0) (index $termList 0) -}} {{/* this block is just for testing */}}
      {{ warnf "eq: true but intersect:%t\t%s\t%s" (intersect $termList .Pages)
                                                  (index .Pages 0)
                                                  (index $termList 0) }}
    {{- end -}}
  {{- end -}}
  {{- $taxoByTerm = uniq $taxoByTerm -}}

  <ul>
  {{- range $index, $term := $taxoByTerm -}}
    <li><a href="{{ $term.Page.RelPermalink }}">{{ $term.Page.Title }}</a></li>
  {{- end -}}
  </ul>

How to reproduce

  1. generate a new site (hugo new site test-site) with a skeleton new theme (hugo new theme test), and put theme = "test" in hugo.toml
  2. in the theme folder, open the three posts and add two different categories (like, article and post)
  3. in themes/layouts, create term/list.html and copy content from _default/list.html
  4. before closing the define "main", put the code above
  5. run hugo server, navigate to localhost:1313/categories and then to one of the categories
  6. the list of tags used in that category will not appear, because it’s empty
  7. Hugo will have logged something like eq: true but intersect:[] Page(/posts/post-3) Page(/posts/post-3)

This is obviously a bug; I will create a GitHub issue.

This is a good alternative, and I think it’s a bit easier to understand:

{{/* Build unique slice of term pages. */}}
{{ $taxonomy := "tags" }}
{{ $s := slice }}
{{ range .Pages }}
  {{ range .GetTerms $taxonomy }}
    {{ $s = $s | append .Page }}
  {{ end }}
{{ end }}
{{ $s = uniq $s }}

{{/* Render links to term pages, sorted (descending) by the number of pages associated with each term. */}}
{{ with sort $s "Pages.Len" "desc" }}
  <ul>
    {{ range . }}
      <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a> ({{ .Pages.Len }})</li>
    {{ end }}
  </ul>
{{ end }}
1 Like

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

@jzeneto I’ll mark this solved once I’ve logged the bug. I’m glad the alternative is working for you.

1 Like

See https://github.com/gohugoio/hugo/issues/12254.

1 Like