# Use shortcode within shortcode definition

**URL:** <https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591>\
**Category:** feature\
**Created:** [May 8, 2019, 1:59pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591 "2019-05-08T13:59:04Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![dktue](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dktue/32/8653_2.png) [@dktue](https://discourse.gohugo.io/u/dktue)\
**Post date:** [May 8, 2019, 1:59pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/1 "2019-05-08T13:59:05Z")

</div>

I would like to request a feature, namely the possibility to [use a shortcode within the definition of another shortcode](https://stackoverflow.com/questions/55401755/use-shortcode-within-definition-of-shortcode/55441113#55441113).

Is this the right place to talk about this feature?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [May 8, 2019, 2:37pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/2 "2019-05-08T14:37:42Z")

</div>

You can start by explaining what you mean exactly.

---

<div class="post-metadata">

**Author:** ![kascme](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kascme/32/6816_2.png) [@kascme](https://discourse.gohugo.io/u/kascme)\
**Post date:** [May 8, 2019, 2:46pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/3 "2019-05-08T14:46:58Z")

</div>

You can call [partials](https://gohugo.io/templates/partials/) from a shortcode. So you could reuse that partial in different shortcodes. However, you cannot call a partial directly from the markdown file, it always then needs to be wrapped by a shortcode.

Was that were your idea was going?

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [May 8, 2019, 2:48pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/4 "2019-05-08T14:48:33Z")

</div>

> [@kascme](#):
>
> However, you cannot call a partial directly from the markdown file, it always then needs to be wrapped by a shortcode.

That is no longer quite true. One can call a partial directly from a content file by enabling and using [Inline Shortcodes](https://gohugo.io/templates/shortcode-templates/#inline-shortcodes)

---

<div class="post-metadata">

**Author:** ![dktue](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dktue/32/8653_2.png) [@dktue](https://discourse.gohugo.io/u/dktue)\
**Post date:** [May 8, 2019, 4:14pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/5 "2019-05-08T16:14:12Z")

</div>

I defined a shortcode `img` that works exactly like the HTML `img`-tag (`{{< img src='...' >}}` but then resizes the image to multiple sizes and outputs them into a `srcset`. I want to reuse this snippet within other shortcodes that make use of images.

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [May 8, 2019, 4:50pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/6 "2019-05-08T16:50:30Z")

</div>

To discuss this, you’ll need to provide examples and more details on what you want. We are open to discuss, but so far no one can deduce what you are suggesting. 🙂

---

<div class="post-metadata">

**Author:** ![kascme](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kascme/32/6816_2.png) [@kascme](https://discourse.gohugo.io/u/kascme)\
**Post date:** [May 8, 2019, 4:55pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/7 "2019-05-08T16:55:39Z")

</div>

> [@alexandros](#):
>
> That is no longer quite true. One can call a partial directly from a content file by enabling and using [Inline Shortcodes](https://gohugo.io/templates/shortcode-templates/#inline-shortcodes)

You are right, I though if it’s about reusing then a proper shortcode is the better approach, as the inline shortcode’s scope is only the content file itself.

> [@dktue](#):
>
> I defined a shortcode `img` that works exactly like the HTML `img` -tag ( `{{< img src='...' >}}` but then resizes the image to multiple sizes and outputs them into a `srcset` . I want to reuse this snippet within other shortcodes that make use of images.

I guess a partial will work quite well. Remeber about the dot `.` and the possibility to pass variables in a `dict` to a partial. Also partials can [return a value](https://gohugo.io/templates/partials/#returning-a-value-from-a-partial). The best way is hard to guess without more information

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [May 8, 2019, 5:20pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/8 "2019-05-08T17:20:43Z")

</div>

> [@kascme](#):
>
> I though if it’s about reusing then a proper shortcode is the better approach, as the inline shortcode’s scope is only the content file itself.

One can always call a reusable partial through Inline Shortcodes in select content files.

On my end at least template partials have always felt way faster when publishing a project than when using plenty of shortcodes in content files.

But now Inline Shortcodes blur the line between the two and they’re interesting because one gets to use a partial in select content files without paying the performance penalty that typically comes with shortcodes.

---

<div class="post-metadata">

**Author:** ![dktue](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dktue/32/8653_2.png) [@dktue](https://discourse.gohugo.io/u/dktue)\
**Post date:** [May 9, 2019, 9:31am UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/9 "2019-05-09T09:31:14Z")

</div>

The use case is the following: I want to create a gallery-shortcode wich takes a directory containing all pictures as a parameter `{{< gallery my-pictures >}}`. Within the definition of the shortcode I want to reference the other shortcode (`{{< img src='...' >}}`) because that way all pictures in the gallery would get proper `srcset`-attributes and responsive images.

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [May 9, 2019, 6:34pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/10 "2019-05-09T18:34:59Z")

</div>

I believe in this instance the best scenario would be to copy the code you want from the `img` shortcode into your `gallery` shortcode.

I know it isn’t DRY, but I think it is easier to manage a shortcode that generates it’s own output. Down the line you’ll save yourself trouble when shortcodes start getting customized and messing with parsing all over the place.

That’s one opinion. It doesn’t really get into the nested shortcode use, but I’ve yet to see a case where it was a good idea. 🙂

---

<div class="post-metadata">

**Author:** ![dktue](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dktue/32/8653_2.png) [@dktue](https://discourse.gohugo.io/u/dktue)\
**Post date:** [May 10, 2019, 8:15am UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/11 "2019-05-10T08:15:40Z")

</div>

I was hoping for a DRY-compatible answer – but hopes were shattered. ☹

---

<div class="post-metadata">

**Author:** ![dktue](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dktue/32/8653_2.png) [@dktue](https://discourse.gohugo.io/u/dktue)\
**Post date:** [May 10, 2019, 8:16am UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/12 "2019-05-10T08:16:09Z")

</div>

But maybe there can be new hope: Can we make this a feature-request?

---

<div class="post-metadata">

**Author:** ![HenrySkup](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/henryskup/32/8183_2.png) [@HenrySkup](https://discourse.gohugo.io/u/HenrySkup)\
**Post date:** [May 10, 2019, 8:50am UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/13 "2019-05-10T08:50:32Z")

</div>

Seems like this might be a major conceptual decision.

It would be good to get clarity/streamlining on the roles of : shortcodes vs. partials vs. templates (internal templates / Go templates ( both the Hugo “layouts” and the {{ template “name” }} … {{ define “name” }} functionality )) vs. blocks .

If we are honest, they are all doing very similar things. Are all still useful? in what instances? are some meant to be interfaces for end users or within a development context? are some a subset of others?

This could also just be a distilling of what is the most “Hugonic” way of doing things.

---

<div class="post-metadata">

**Author:** ![dktue](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dktue/32/8653_2.png) [@dktue](https://discourse.gohugo.io/u/dktue)\
**Post date:** [May 10, 2019, 12:01pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/14 "2019-05-10T12:01:33Z")

</div>

@HenrySkup: You’re absolutely right – the lines between those blur. For me, shortcodes have always been the only way an author should be able to structure it’s content. That’s why I would want to use shortcodes in shortcodes to create powerful tools for them.

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [May 10, 2019, 3:05pm UTC](https://discourse.gohugo.io/t/use-shortcode-within-shortcode-definition/18591/15 "2019-05-10T15:05:35Z")

</div>

Hi @dktue. If I’m understanding your question, you should be able to extract the code from your img shortcode into a partial, and then call that partial from your img and gallery shortcodes. (As others have mentioned/hinted).

If my understanding is off, then you need to share your code that way we can work with something concrete. Else we’re living in idea-land.
