Hugo

Shortcode and nested shortcodes with variables

I’m considering how to add a shortcode that wraps a set of shortcode invocations like:

{{< overall prefix="myprefix" >}}
  {{% myinner url="/url1/img1.png" %}}
  {{% myinner url="/url2/img2.png" %}}
{{< /overall >}}

And I’m wondering If i can “pass” the prefix along to each of the shortcodes in .Inner.

I’ve tried (in shortcode/overall.html)

$.Scratch.Add "prefix" (.Get "prefix")

and then in myinner.html

$.Scratch.Get "prefix"

but it’s empty then.
Is it possible to do?

The idea is to have a prefix in the wrapping function and reuse it for all the inner shortcodes instead of explicitly adding them it as params.

No. The shortcodes are rendered from the inside then out. Doing it the other way is hard to wrap my brain around … Passing parameters from outer to inner should be possible to implement, but isn’t currently.

1 Like

The above should fix this. It is work in progress (missing tests and doc), but is simple enough.

{{< outer p1="p-val" >}}
{{< inner >}}
{{< /outer }}

And in inner:

Parent: {{ .Parent.Get "p1" }}

Would this solve your use case, @svrist ?

Exactly.
I got it working by this crude hack:

<!-- in outer.html  -->
  {{ replace .Inner "PREFIX" (.Get "prefix") | safeHTML }}

and

<!-- inner.html -->
<img src="PREFIX/{{ .Get "file" }}" />