Using intersect to find related content

I’m trying to use intersect to make a list of related content. With a fresh hugo build and looking at the (dev) documentation for it (see ).

I have the following code in a single.html

{{ $page_link := .Permalink }}
{{ $oggetti := .Params.oggetti }}
{{ range .Site.Recent }}
   {{ $page := . }}
   {{ $has_common_tags := intersect $oggetti .Params.oggetti | len | lt 0 }}
   {{ if and $has_common_tags (ne $page_link $page.Permalink) }}
       <li><a href="{{ $page.Permalink }}">{{ $page.Title }}</a></li>
   {{ end }}
{{ end }}

Where oggetti is the name of the taxonomy I’m using and all content has at least one term listed in front matter.
But I just get an error for each page of content, example:

ERROR: 2014/11/06 Error while rendering page foto/foto/ template: foto/single.html:26:43: executing "foto/single.html" at <intersect $oggetti ....>: error calling intersect: can't iterate over string

Any idea what I’m doing wrong?

1 Like

Intersect takes two arrays or slices as input and is obviously getting a string.

Just tested this by replacing .Params.ogetti with .Params.categories - works fine (I have no ogetti).

I would double check the ogetti and make sure that it’s really a Taxonomy.

It was indeed a taxonomy. But I double checked the documentation for syntax of taxonomies and terms, it turns out the problem was the way I’d specified terms in the content front matter. I’d only added square brackets around terms when there was more than one, not for single terms. Which I presume is why they were seen as a string instead of an array.
So my front matter had

oggetti = "fiori"

which I changed to

oggetti = [ "fiori" ]

And the errors were gone. Thanks for the prompt to check the taxonomy!

1 Like

I know with some other things we’ve actually written code to accept either a single string value or an array of string values and internally convert it into a slice of strings. We should look into doing this for taxonomies as well.

1 Like

Added an issue on github

I read codes. Taxonomies itself is built in assembleTaxonomies function in hugolib/site.go. It assembles both a single string definition and a string slice definition into a taxonomy slice. The other hand, the front matter value is saved as a single string Param value in update function in hugolib/page.go. So in this case, to use it with intersect, the front matter value should be bracketed off.

Just tested this code using the .Tags parameter and it works GREAT!