[SOLVED] How to create union list of terms of two taxonomies?

In the template \layout\taxonomy\person.terms.html I add this strings:

{{ $authors := $.Site.Taxonomies.authors }}
{{ $recipients := $.Site.Taxonomies.recipients }}
{{ $un1 := union $authors $recipients }}

Hogo 0.31.1 generate error at <union $authors $reci...>: error calling union: can't iterate over hugolib.Taxonomy

I think the taxonomy map can’t be unioned because it is not an array or slice (types).

However, I was able to use scratch and a loop to collect specific terms, test below hope this helps as a solution, if you were merging to pass to something else (template, etc), not sure of how you are using “$un1”.

Example:
I use var shortcut to .Scratch.

{{ $var := .Scratch }}

<!-- Collect Specific cateogries -->
{{ range $taxName, $terms := .Site.Taxonomies.authors }}
    {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" . ))}}
{{ end }}
{{ range $taxName, $terms := .Site.Taxonomies.recipients }}
    {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" . ))}}
{{ end }}

<!-- Test (presumably your template) -->
{{ range ($var.Get "mergedTax") }}

    <!-- Terms key -->
    <p><strong>{{ .termName }}</strong></p>

     <!-- Terms map (weighted pages, if needed) -->
    <p>{{ .term.Pages }}</p>
{{ end }}

Notes:

  • Using .Scratch.Add to push into an array (collection)
  • Using “slice” to indicate this is an array item for the “.Add” method
  • Using dict to collect the term similar to it’s original map structure (to get the key and the map)
  • Looping though the final created array or dictionary items (key/value pairs) for testing that all were collected

Hope this helps…

2 Likes

Thank you, your code works well. But I can not remove duplicate terms.

First I tried:

{{ range sort (uniq ($var.Get "mergedTax")) "termName" }}

The sort works, but the uniq does not work and does not generate an error.

Then I tried it in the second range:

{{ range $taxName, $terms := .Site.Taxonomies.recipients }}
      {{ if not (in ($var.Get "mergedTax") $taxName) }}
            {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" .))}}
      {{ end }}
{{ end }}

The in does not work and does not generate an error.

Maybe you have any ideas for removing duplicates?

I think your approach would work if we get the names into an array by themselves. The “mergedTax” array is an array of dictionaries (like objects). Below I wrote out what this would look, I added another array to collect the names to check if they had been collected yet. This code is untested…

{{ $var := .Scratch }}

<!-- Collect Specific cateogries -->
{{ range $taxName, $terms := .Site.Taxonomies.authors }}
  {{ $var.Add "mergedTaxNames" (slice $taxName) }}
  {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" . ))}}
{{ end }}
{{ range $taxName, $terms := .Site.Taxonomies.recipients }}
    {{ if not (in ($var.Get "mergedTaxNames") $taxName) }}
        {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" . ))}}
    {{ end }}
{{ end }}

<!-- Test (presumably your template) -->
{{ range ($var.Get "mergedTax") }}

    <!-- Terms key -->
    <p><strong>{{ .termName }}</strong></p>

    <!-- Terms map (weighted pages, if needed) -->
    <p>{{ .term.Pages }}</p>
{{ end }}
1 Like

Thanks again. A combined list of terms for the three taxonomies. The final code that generates hyperlinks.

{{ $var := .Scratch }}

<!-- Collect Specific cateogries -->
{{ range $taxName, $terms := .Site.Taxonomies.authors }}
    {{ $var.Add "mergedTaxNames" (slice $taxName) }}
    {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "path" "authors"))}}
{{ end }}
{{ range $taxName, $terms := .Site.Taxonomies.recipients }}
    {{ if not (in ($var.Get "mergedTaxNames") $taxName) }}
        {{ $var.Add "mergedTaxNames" (slice $taxName) }}
        {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "path" "recipients"))}}
    {{ end }}
{{ end }}
{{ range $taxName, $terms := .Site.Taxonomies.persons }}
    {{ if not (in ($var.Get "mergedTaxNames") $taxName) }}
        {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "path" "persons"))}}
    {{ end }}
{{ end }}

<ul>
{{ range sort ($var.Get "mergedTax") "termName" }}
    <li><p><a href="/{{ .path }}/{{ urlize .termName }}/">{{ .termName }}</a></p></li>
{{ end }}
</ul>
2 Likes

Hi all, I’m building a portfolio site and the solution you’ve come up with seems like what I would need to achieve the attached UI. Where selecting and deselecting each of the filter buttons at the top would shrink or grow the list by showing content that matches all of the currently selected filters. I’m still in the design phase here am trying to work out if what I’m after is even possible. I’m thinking I would add a block above for each permutation that my taxonomy items create, then stash the URL until the selecting/deselecting matches one.

Hi szac,

Probably to late to help! but… you may consider using javascript to achieve the filters depending on how many items are on the page. Since you are already printing them all out to the page to begin with and then filtering down. This would be easy by printing the taxonomies for each item as data attributes.

I guess if there are tons of the items you may look for a different approach.

Thanks Scherb

Yep, that’s pretty much what I ended up doing. My thinking here was for pagination and deep linking to search sets of multiple filters. This should be possible too with pushState but I haven’t messed around with it.

Ok cool