Hugo union with priority levels

Hello,

I am using union to combine images from multiple folders but I would like to keep the control over priority of images how can achieve this.

My current code:

# in page front matter I define a folder where all the images are kept such as
---
imagesResources: "images/cars/**/*"
---

then I have this layout which takes all the images from images and the folder defined in the param and union them to use on the main page like this:

{{ $imagesResources := (union (resources.Match "images/all/*") (resources.Match (.Site.GetPage "/all").Params.imagesResources)) }}

{{ $sectionName := .Site.Params.folderName }}
{{ if not $sectionName }}
{{ $sectionName = "cars" }}
{{ end }}

{{ $imageResources := (union (resources.Match (printf "images/%s/*/*" $sectionName)) (resources.Match (.Site.GetPage (printf "/%s" $sectionName)).Params.imagesResources)) }}


{{ $count := 8 }}
{{ with .Params.noOfGalleryImages }}
{{ $count = . }}
{{ end }}

on the main page where I use the gallery I have the front matter

---
noOfGalleryImages: 12
preferedGallery:
 - images/somefolder/image1.jpg
 - images/somefolder/image2.jpg
---

what I am not able to work out is

  1. Use the preferred images first and then take images from other sources to match the count provided in the layout
  2. make sure the images are not duplicated.

the preferred images comes from the same source I just want to show them higher. currently I drop all images in different folders and dynamically generating galleries. This way I do not have to define 90 images in the page front matter.

Thank you!

To summarize, are you simply asking how to get the desired order with the union function? If so…

{{ union (slice 2 1 3) (slice 5 3 4) }} --> [2 1 3 5 4]

{{ union (slice 5 3 4) (slice 2 1 3) }} --> [5 3 4 2 1]
1 Like

Yes please, Following the approach demonstrated in the initial example, enabling Hugo to initially select images specified in the page parameter according to their weight. Then, proceed to apply additional rules defined in the Hugo layout, ensuring that duplicates of already selected images are excluded, mirroring the example you provided above.

How can I achieve this?

Thank you