I’m just tinkering with an image gallery shortcode I wrote a while back, to try and make it a bit more user friendly. At present it takes the following format:
{{< gallery
"Gallery Name"
"image01filename.jpg|Caption for Image 01"
"image02filename.jpg|Caption for Image 02"
"image03filename.jpg|Caption for Image 03"
>}}
I’m using the following [clunky!] code to extract the image filename and accompanying caption into two variables, which I then use when building the gallery code:
{{ range $images }}
{{ $imageandcaption := split . "|" }}
{{ $image := index $imageandcaption 0 }}
{{ $caption := index $imageandcaption 1 }}
<!--- HTML stuff -->
{{ end }}
It works but, as I said, it’s a bit clunky. Not least because I have to remember to use that pipe character between filename and caption, when adding the shortcode to a page: It would be more elegant if I could just use a space:
"image01filename.jpg Caption for Image 01"
But obviously splitting on a ‘space’ I’d end up with each word of the caption as a separate slice element. What I’d want to do is something like:
{{ $image := index $imageandcaption 0 }}
{{ $caption := index $imageandcaption 1: }}
using the Golang slice[1:]
syntax to put everything after the first space into the $caption
variable. But the parser doesn’t like that.
Is there a way of doing this in Golang template syntax, or am I stuck with using some kind of ‘unusual’ character as a delimiter to seperate filename and caption?