I’m pretty new in Hugo and maybe it’s a simple cuestion but I can’t find solution. How can I pass dict or array variables to my custom shortcode?
I try with this one but it doesn’t work
{{< gallery data="["blue.jpg", "red.jpg"]" >}}
In a short code I want to loop over the images to display them
{{ range $array }}
<img src='gallery/'{{ . }}' />
{{ end }}
The next question is: How to send “alt” for each image? Dict like: {"images": [{"image": "image1.jpg", "alt": "Alternative text for image 1"}, {"image": "image2.jpg", "alt": "Alternative text for image 2"}]}
{{ range $array }}
{{ $item := split . "|"}}
{{ $image := index $item 0 }}
{{ $alt := index $item 1 }}
<img src="/path-to-gallery/{{$image}}" alt="{{$alt}}">
{{ end }}
It work’s fine and it’s enough to solve my problem. In fact you can pass any arrays in that way. The only thing you have to do is to convert each string element to data. Just take care of delimiters
.Params.caption is within the .Page.Resources loop and therefore refers to your resources.
Just add this to the front matter of the index.md file containing the gallery:
---
resources:
- src: gallery/1.jpg
params:
caption: My Caption **using Markdown**
- src: gallery/3.jpg
params:
caption: Another Test
By using ** in printf "%s**" $dir you can use file names or folders:
So {{< gallery src="gallery1" >}}
works for
gallery1-01.jpg
gallery1-02.jpg
…
and for
gallery1/
I tried the exactly same approach you are doing some time ago. It just did not work for me, and I was looking for a ‘Hugo out of the box’ solution. That’s why I am mentioning this approach.