Use front matter defined variables as argument in custom shortcode

Hi all,

I have a shortcode defined which takes an argument, I’ve called the argument “title”. It’s called like this:

{{< post/title title="Some title" >}}

All is well.

But sometimes I want to pass the Page.Title as an argument. And this is where I’m stuck. How do I do that? This doesn’t work:

{{< post/title title=.Title >}}

I’ve tried several variants like passing {{.Title}} or with double quotes but nothing seems to help. I have the feeling I’m close, can someone nudge me in the right direction?

Thank you in advance,
Harm

You cannot include template code (including shortcode invocations) within a shortcode argument. For example, these are not allowed:

{{< post/title arg="{{ .Title }}" >}}
{{< post/title arg="{{< param `Title` >}}" >}}

Rework your shortcode.

Option 1: Use Inner instead of argument

markdown

{{< post/title >}}
Some title
{{< /post/title >}}

{{< post/title >}}
{{< param "Title" >}}  # https://gohugo.io/content-management/shortcodes/#param
{{< /post/title >}}

shortcode

{{ .Inner }}

Option 2: Use default

markdown

{{< post/title title="Some title" >}}

{{< post/title >}}

shortcode

{{ default .Page.Title (.Get "title") }}
1 Like

Thanks for ending this search for me. And double thanks for the two(!) solutions.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.