Range over the children of a Shortcode

Right. There are two ways you can go about it:

  1. Inline Shorcode with Nested Partial in which you range over the .Page.Resources provided that you organize the content in Page Bundles.

  2. Splitting the rendered .Inner content of the Nested Shortcode that you already have based on its newlines and then ranging over the rendered images.

It’s up to you which approach you want to follow.

The first requires re-organizing the content, enabling Inline Shortcodes, but offers the possibility of the full template power from within the content file and it scales nicely.

The second respects your current project structure and shortcodes but I am not sure how it will scale.

Since this topic is about Ranging over the children of a Nested Shortcode (and that is why I added the word Nested in the topic title) I will offer a way to do just that:

In content-highlight.html

{{- with .Inner -}}{{- $inner := split . "\n" -}}{{- range $i, $e := $inner -}}{{ $img := . }}{{ if eq $i 5 }}<--- copy to be rendered after the 4th img -->{{- end -}}{{ $img | safeHTML -}}{{ end }}
{{- end -}}

In the above the .Inner contents are splitted by \n newline.
The rest is self explanatory.
In the shortcode template, everything needs to be in one line to avoid redundant newlines that will complicate things.

2 Likes