[SOLVED] Can I check if a shortcode is inside another shortcode?

Sometimes I find useful to place one shortcode inside another. E.g.:

{{< gallery >}}
  {{< img url1 >}}
  {{< img url2 >}}
{{< /gallery >}}

It works fine. However, I use {{< img >}} shortcode both inside and outside the {{< gallery >}} shortcode. I’d like to set some parameters of the img shortcode in shortcodes/img.html depending on whether it is inside the gallery shortcode or not. I looking for something like:

{{ if inside gallery }}
...
{{ end }}

Is there a way to check this?

You can do something like this in img:

{{ with .Parent }}
  // This will now be the gallery shortcode if inside one
{{ end }}

@rdwatters I remember adding this feature, but I’m pretty sure I forgot to document it …

1 Like

Not a problem. I seem to recall making notes about .Parent somewhere, so I can review, but nonetheless for safekeeping:

Thanks, Bjorn!

Thanks @bep, it is exactly what I was looking for and got answer just in 5 minutes. Much appreciated!

1 Like