I’m trying to create a shortcode to render images with a JS image gallery script I’ve got. The JS is pretty straightforward. It just looks for a <div class="gallery">
and builds a gallery from any images and their thumbnails inside that div. eg:
<!-- gallery box -->
<div class="gallery">
<a href="grafix/test01.jpg" data-caption="test01"><img src="grafix/test01wee.jpg" alt="test01"></a>
<a href="grafix/test02.jpg" data-caption="test02"><img src="grafix/test02wee.jpg" alt="test02"></a>
<a href="grafix/test03.jpg" data-caption="test03"><img src="grafix/test03wee.jpg" alt="test02"></a>
</div>
<!-- end gallery box -->
I can make a simple shortcode like this:
{{< gallery "test01" "test02" "test03" >}}
and create the shortcode template thus:
<!-- gallery box -->
<div class="gallery">
{{range .Params}}
<a href="grafix/{{.}}.jpg" data-caption="{{.}}">
<img src="/grafix/{{.}}wee.jpg" alt="{{.}}"></img>
</a>
{{end}}
</div>
<!-- end gallery box -->
and that gets me the bare-bones of what I want. But there are some problems:
1: The .jpg
and wee.jpg
[for the thumbnails] filename endings are hard-wired in, which is exceedingly crap. What if the gallery comprised pngs
or gifs
?
2: If there is more than one instance of the gallery div on a page, I need to give each one an id
to uniquely identify it, so the script keeps each gallery separate.
In an ideal world, my shortcode would look something like this:
{{< gallery galleryname="somegallery" imagetype="jpg" "image01" "image2" "image3"... >}}
but that would fall foul of not being able to mix named vs positional parameters in a shortcode.
I could have all the parameters positional and then extract the galleryname
and imagetype
as the first two items but then how do I range through the rest of them? It doesn’t seem possible using Golang template code to slice an array by doing something like {{ range .Params[2:] }}
to loop through the rest of the parameters.
Alternately, using named parameters and putting the list of images into an array. Something like:
{{< gallery name="somegallery" imagetype="jpg" "images=["image01","image2","image3"...] >}}
would seem like a potential approach. But trying something like that throws up a multitude of errors.
So, can anyone think of a way this might be doable? Even just knowing whether or not it’s possible to pass an array as a named parameter, or slice an array of positional parameters would be a start.