Return unique values from front matter

I have several hundred products with the following front matter:

countries = []
description = ""
title = ""

I need to generate a list of unique countries used and a code reference for each. As an example with the following products:

countries = ["Australia", "Canada"]
description = ""
title = ""

countries = ["Australia"]
description = ""
title = ""

countries = ["Belgium", "New Zealand"]
description = ""
title = ""

I would like the list to be:

country_id: "c_australia",
country: "Australia"

country_id: "c_belgium"
country: "Belgium",

country_id: "c_canada"
country: "Canada",

country_id "c_new_zealand:
country: "New Zealand"

I’ve tried doing:

{{range .Site.RegularPages}}
{{.Params.countries}}
{{end}}

which gives the list:

["Australia", "Canada"]
["Australia"]
["Belgium"]
["Canada", "New Zealand"]

This is not what I want as it repeats Australia twice. I tried adding uniq to my code like so:

{{range .Site.RegularPages}}
{{.Params.countries | uniq}}
{{end}}

But no change. I’m also not sure how to break up each country and add the country_id. Any help please?

Have you tried with?

Yes. I’m not sure how that helps. I get the same output as shown in my previous post.

Can you elaborate on how I should be using with?

1 Like

Could you use a taxonomy? Then when hugo compiles it will build a list of all the terms (countries) and the pages they are used on.

They are really simple to setup and use, you would just need to add it to the config file as it pulls from the pages Params. You just need to tell it that that specific param is a taxonomy “countries” in the config. Then it builds the list for you. It is available in the page template but contains the aggregate list of terms used on all pages.

If you needed to create a unique ID for a term when it is used you could either use the same formatting method to create it “c_{{$country | urlize }}” or use scratch to create the secondary list of id’s (ie. “Australia” : “c_australia” ) when looping through the taxonomy hugo provides.