Hello there,
I’ve searched in multiple directions by now, but unless I missed things, I believe I have not seen any chance to be able to recall a shortcode whose arguments can be the output of another shortcode.
I’d love to have something dynamic like that in a content (.md) file like:
{{% test {{% inner-sh "something"}} %}}
But the above fails with this error code unrecognized character in shortcode action: U+007B '{'. Note: Parameters with non-alphanumeric args must be quoted
.
Useless to say that putting the second shortcode in quotes let Hugo treat it as a string, so I can’t reach my goal.
I’ve tried resorting to functions without success too.
Is this that can’t be achieved at all?
From this answer you can use partial with return statement to do some computation, dont know if it’s exactly what you are looking for, but it’s a lead
1 Like
Thanks. I’ve went for that solution, just to later find out that it doesn’t still tick my boxes.
I’m still in a position to make say a img shortcode (or anything with dynamic values) depending on the page to manipulate the output.
So, something like the below works fine; {{% img src="img/twitter-icon.png" alt="Twitter" %}}
but as soon as I want the alt parameter being dynamic, that’s where I get stuck.
Now, I tried with the nested shortcode, having something like {{% img % }}Something{{% /img }}
but that’s a new challenge. How can I get the {{ .Inner }} being assigned back to my named parameter?
My img
shortcode doesn’t do anything special, but as soon as I mix up code that try to get access to the {{ .Inner }} context with the existing code, I start get all sort of errors during compilation. Nothing that really helps me to understand what’s wrrong.
For reference that my img
shortcode code:
{{- $options := cond .IsNamedParams (.Get "src") (.Get 0) | dict "Src" -}}
{{- $options = cond .IsNamedParams (.Get "alt") (.Get 1) | .Page.RenderString | dict "Alt" | merge $options -}}
{{- if .IsNamedParams -}}
{{- $options = dict "Datasrc" (.Get "datasrc") | merge $options -}}
{{- $options = dict "Title" (.Get "title") | merge $options -}}
{{- $options = dict "Height" (.Get "height") | merge $options -}}
{{- $options = dict "Width" (.Get "width") | merge $options -}}
{{- end -}}
{{- $options = dict "Resources" .Page.Resources | merge $options -}}
{{- with $options -}}
{{- partial "plugins/img.html" $options -}}
{{- end -}}
And when I say mix up, I mean altering the code above so that I can do something like {{ with .Inner }} .... {{ else }}
the rest of the code above.
I borrowed the code from a template called LoveIt, and made few changes, but nothing major (and I still don’t have experience in Hugo as you might guess).
The plugin/img.html just prints out the HTML img tag after verifying the img value passed in.