Can i call a shortcode from a partial?

Hi there,

as described in title: is it possible to call a shortcode in a partial?

If i just use the normal {{< >}} syntax i got a parse error.

parse failed unexpected "<" in command

many thanks!

No, you can not.

But it is possible to call a partial from a shortcode.

1 Like

Okay.
Is it possibile to append the shortcode string to the end of the content befor rendering?
I want to display a (3rd party) gallery, which users can add to the end of a post to display pictures from the images/. folder.
To make their lifes easier it want to add the shortcode automatically, if a flag is set in netlify cms.

With “flag is set in netlify cms” I assume you refer to a frontmatter parameter.

I suggest you add a if statement to your template. Check if the parameter is set and if so add the gallery.

1 Like

Exactly that was my idea. But how to append the gallery shortcode to the post-content :slight_smile:

No.

It sounds like you need to refactor your shortcode as a partial, then programmatically call the partial from the template if the flag is set.

A common construct is to:

  1. Create a partial (to be called from templates)
  2. Create a shortcode that wraps the partial (if you need to call it from content)

layouts/partials/hello.html

<p>Hello {{ .name }}!</p>

layouts/shortcodes/hello.html

{{ partial "hello.html" (dict "name" (.Get "name")) }}

content/post/foo.md

{{< hello name="John Doe" >}}
2 Likes

I had that in mind too but wanted to avoid this, because it’s a 3rd party shortcode. So i cannot easily update it.

Well, i’ll think it over. It has no priority.

Thanks for your help!

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