How to pass multiple keywords as keyVals to RelatedTo

I am trying to make a shortcode that pulls in related pages based on a list of topics.

I have related pages working elsewhere in the site, but this is tricky because it is from a specific list using .RelatedTo.

The shortcode is

{{< list-resources topics="design, content" >}}

It works if I hard code in the topics into the keyVals, like this:

# Gets the list of $related_pages based on the topics 
{{- $related_pages := $.Site.RegularPages.RelatedTo ( keyVals "topics" "design" "content") -}}
# Passes this data to the list_related_resources partial that will make the list
{{ partial "core/list_related_resources.html" (dict "Page" .Page "RelatedPages" $related_pages) }}

But if I try to pass the array (slice) to the KeyVals, I get 0 $related_pages pages.

# Gets the list of topics passed through the shortcode
{{- $topics := (slice (.Get "topics")) -}}
# Gets the list of $related_pages based on the topics 
{{- $related_pages := $.Site.RegularPages.RelatedTo ( keyVals "topics" "design" "content") -}}
# Passes this data to the list_related_resources partial that will make the list
{{ partial "core/list_related_resources.html" (dict "Page" .Page "RelatedPages" $related_pages) }}

Any thoughts?

I’m guessing what you want to do isn’t currently possible. the “vals” argument is a Go vararg, so sending a slice into it doesn’t work (or: it does not do what you want).

I saw a simlilar question in another thread about “slice expansion” or what you in Go would note as myslice..., and I guess we should add a helper function or something to handle this. But you need to create a GitHub issue.

Thanks — yes, I will create an Issue for this.

In the meantime, I might try creating a range that loops through each topic and gets the related pages and push them to a common var.

Or maybe I will try converting the array to a string "design" "content" and to see if that works.

Did you have any luck solving your problem?

1 Like