Shortcodes in template?

Is there a way to call shortcodes in a template?

Specifically, in a template, I would like to embed some videos dynamically from a collection.

Thanks.

You can create partials for your shortcodes and reuse those in templates. I call them components.

Thanks for your response. Yes, is what I ended up doing. But I wonder if there is a cleaner way without duplicating code.

That’s going to confuse folks with theme components. May be you rename those with something else… Like “nuggets”? “legos”? “atoms”? :slight_smile:

1 Like

Good point! Snippets it is. :sweat_smile:

Thanks for the nudge on naming. I’ve updated the snippet docs in my theme to include information and several examples of this pattern ranging from basic button to the more exotic buttongroup and simple language to help drive the concept home.

Please take a look at the doc above and let me know if you see anything duplicated. The pattern I’m using is essentially a composite composition which delegates all markup and logic to a partial, making the shortcode an interface and nothing more. Arguably some of the identifiers are duplicative but that was a design choice.

I’m not talking about your code. Sorry for the confusion. I meant without duplicating the hugo code. There’s a already a builtin youtube shortcode in hugo, it doesn’t make sense duplicating its code to embed youtube videos in a template. That’s why I’m asking if there is a way to use that shortcode in a template.

I think what @jhabdas means is you don’t have to duplicate the code if your use your partial from within your shortcode template. But I agree this is not ideal as you have to test all your shortcode parameter before passing them as a dict to your partial context.

I for one would also love to have a way to call shortcodes from within the templates or at least calling layouts files using the same convenient “shortcode params” logic (.Get etc…). But this involves a lot of specing as you would need “private” snippets limited to your template usage VS “public” snippets (current shortcodes) available to your editors VS “any” snippets available to both.

If you have a design in mind to managing this, feel free to open a thread detailing it and everyone can discuss it there.

1 Like

Ah that would be handy. I believe similar discussions have come up in the past. Please forgive me for not linking as I’m on a small screen.

Looking at Gutenberg Shortcodes I can see they’ve made them available (to the templates too?). Seems Hugo could make a shortcode function and let the templating language used handle the semantics, allowing one the ability to leverage things like the privacy config without having to duplicate that logic too.

Yes conventional WordPress also have a do_shortcode function. I never thought about it this way…

1 Like