Accessing Sub-Slice From Element n to End of a Slice

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?

Totally untested, but you get the idea:

{{< gallery
    "Gallery Name"
    "image01filename.jpg" "Caption for Image 01"
    "image02filename.jpg" "Caption for Image 02"
    "image03filename.jpg" "Caption for Image 03"
{{ $.Scratch.Add "counter" 0 }}
{{ range seq(div len($images) 2) }}
{{   $n       := mul ($.Scratch.Get "counter") . }}
{{   $image   := index $images $n }}
{{   $caption := index $images (add $n 1) }}
{{   $.Scratch.Set "counter" . }}
<!--- HTML stuff -->
{{ end }}
{{ $caption := index (after 1 $imageandcaption) }}
1 Like

Cheers guys!

Unfortch, neither of those worked “out of the box”…

Parser didn’t like the brackets in @moorereason’s line: {{ range seq(div len($images) 2) }}

ERROR: 2016/09/13 14:55:52 template.go:472: template: theme/shortcodes/gallery.html:10: unexpected "(" in operand

and it was equally sniffy about @bep’s {{ $caption := index (after 1 $imageandcaption) }} line:

ERR: template: theme/shortcodes/gallery.html:13:22: executing "theme/shortcodes/gallery.html" at <after 1 $imageandcap...>: erro
r calling after: no items left

However. Oddly. In spite of those errors, with the latter, I was able to echo the $caption value to screen [albeit it seemed to be formatted as a slice] and also to successfully integrate it into the HTML. So it seems to work, even though the parser doesn’t like it.

Now I just need to either strip the ‘[ ]’ if it’s a string, or cast it to a string, if it’s a slice.

Yeah, that was sloppy. Maybe this…

{{ range seq (div (len $images) 2) }}