Using page params with shortodes

Hello. I’m trying to use a page param with a shortcode call. Its a more or less standard page. The template I’m using has a button shortcode and I want to pass the link parameter to the shortcode call.

I would prefer to avoid creating new shortcodes, but if it is required I would like to reuse the actual code.

Here is a exemple of my content page to show you what I’m trying to do.

---
title: "Example"
date: 2017-06-16
url: "https://example.com/page"
---
{{< button href='.param "url" ' >}}External Link{{< /button >}}

You could just create a shortcode for button you want to render with the shortcode, let’s call it url-button.html, and then pass the url parameter from that, then call the shortcode in the post.

post-name.md
{{ < url-button > }}

url-button.html
<a href="{{ .Page.Params.url }}" class="button">Button</a>

1 Like

I was trying to avoid that solution because I dont want to have such a similiar code in two different shortcodes because it could became a pain to make some changes but your sugestion definitely its an options to fix this problem

What you can do is pass the param name in:

{{< url-button “url” >}}

url-button.html
<a href="{{ index .Page.Params (.Get 0) }}" class="button">Button</a>

2 Likes