# Expand shortcodes (or partials) within custom shortcodes

**URL:** <https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466>\
**Category:** support\
**Created:** [November 5, 2021, 9:51pm UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466 "2021-11-05T21:51:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mij](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mij](https://discourse.gohugo.io/u/mij)\
**Post date:** [November 5, 2021, 9:51pm UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/1 "2021-11-05T21:51:12Z")

</div>

Hello folks,

How can you get `shortcodes` (or `partials`) to expand shortcodes within them?

I want to build a shortcode (or partial) for a Disclaimer – to be cited in all posts. The disclaimer uses the [admonition](https://ublogger.netlify.app/theme-documentation-extended-shortcodes/#4-admonition) shortcode offered in theme [uBlogger](https://ublogger.netlify.app/).

The final partial to display is:

```auto
{{< admonition type=warning >}} **Disclaimer** : All information on this page is my opinon and for information purposes only [snip].{{< /admonition >}}

```

As soon as I excise that into an own partial (or shortcode), `hugo` fails to run, with error:

```auto
Process: loading templates: "/Users/mij/mysite/layouts/partials/disclaimer.html:1:1": parse failed: template: partials/disclaimer.html:1: unexpected "<" in command

```

The same applies if I use syntax `{{% %}}` instead.

Thank you!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 5, 2021, 10:07pm UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/2 "2021-11-05T22:07:32Z")

</div>

| Outer | Inner | Allowed |
| --- | --- | --- |
| partial | partial | ✅ |
| shortcode | partial | ✅ |
| partial | shortcode | ❌ |
| shortcode | shortcode | ✅ |

Nesting a shortcode within a shortcode can be tricky—it depends on what each one is doing. You will need to provide code for both if you have further questions.

---

<div class="post-metadata">

**Author:** ![mij](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mij](https://discourse.gohugo.io/u/mij)\
**Post date:** [November 7, 2021, 4:32am UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/3 "2021-11-07T04:32:31Z")

</div>

Thank you @jmooring .

Here’s the code for the outer shortcode (defining my disclaimer):

```auto
{{< admonition type=warning >}} **Disclaimer** : All information on this page ...{{< /admonition >}}

```

And here’s the code for the inner shortcode (defining `admonition`):

```auto
{{- $inner := .Inner | .Page.RenderString -}}

{{- $iconMap := dict "note" "admonition-icon icon-note" -}}
{{- $iconMap = dict "abstract" "admonition-icon icon-abstract" | merge $iconMap -}}
{{- $iconMap = dict "info" "admonition-icon icon-info" | merge $iconMap -}}
{{- $iconMap = dict "tip" "admonition-icon icon-tip" | merge $iconMap -}}
{{- $iconMap = dict "success" "admonition-icon icon-success" | merge $iconMap -}}
{{- $iconMap = dict "question" "admonition-icon icon-question" | merge $iconMap -}}
{{- $iconMap = dict "warning" "admonition-icon icon-warning" | merge $iconMap -}}
{{- $iconMap = dict "failure" "admonition-icon icon-failure" | merge $iconMap -}}
{{- $iconMap = dict "danger" "admonition-icon icon-danger" | merge $iconMap -}}
{{- $iconMap = dict "bug" "admonition-icon icon-bug" | merge $iconMap -}}
{{- $iconMap = dict "example" "admonition-icon icon-example" | merge $iconMap -}}
{{- $iconMap = dict "quote" "admonition-icon icon-quote" | merge $iconMap -}}
{{- $iconDetails := "admonition-icon admonition-icon-arrow-right" -}}
{{- $aside := (.Get `aside`) | default false }}

{{- if $aside -}}<aside>{{- end -}}
{{- if .IsNamedParams -}}
    {{- $type := .Get "type" | default "note" -}}
    <div class="details admonition {{ $type }}{{ if .Get `open` | ne false }} open{{ end }}">
        <div class="details-summary admonition-title {{ if not (.Get `title`) }}admonition-title-none{{ end }}">
            {{- with (.Get "title") -}}
            <i class="icon {{ index $iconMap $type | default (index $iconMap "note") }}"></i>{{ . | default (T $type) }}<i class="details-icon {{ $iconDetails }}"></i>
            {{- end -}}
        </div>
        <div class="details-content">
            <div class="admonition-content">
                {{- $inner -}}
            </div>
        </div>
    </div>
{{- else -}}
    {{- $type := .Get 0 | default "note" -}}
    <div class="details admonition {{ $type }}{{ if .Get 2 | ne false }} open{{ end }}">
        <div class="details-summary admonition-title" style="display: none">
            <i class="icon {{ index $iconMap $type | default (index $iconMap "note") }}"></i>{{ .Get 1 | default (T $type) }}<i class="details-icon {{ $iconDetails }}"></i>
        </div>
        <div class="details-content">
            <div class="admonition-content">
                {{- $inner -}}
            </div>
        </div>
    </div>
{{- end -}}
{{- if $aside -}}</aside>{{- end -}}

```

---

<div class="post-metadata">

**Author:** ![iaeiou](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/iaeiou/32/15332_2.png) [@iaeiou](https://discourse.gohugo.io/u/iaeiou)\
**Post date:** [November 7, 2021, 11:09pm UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/4 "2021-11-07T23:09:13Z")

</div>

You _may_ find interesting bits there: [Hugo: add custom infoboxes - roneo.org](https://roneo.org/en/hugo-custom-warning-infoboxes/)

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 8, 2021, 12:16am UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/5 "2021-11-08T00:16:30Z")

</div>

When I described inner and outer shortcodes, I was referring to nested shortcodes. For example:

config.toml

```auto
[markup.goldmark.renderer]
unsafe = true

```

layouts/shortcodes/disclaimer.html

```auto
This is *bold* text before the admonition.
{{ .Inner }}
This is *bold* text after the admonition.

```

markdown

```auto
{{% disclaimer %}}
{{< admonition type="warning" title="Foo" aside=true >}}
**Disclaimer** : All information on this page ...
{{< /admonition >}}
{{% /disclaimer %}}

```

You cannot call a shortcode from a shortcode.

---

<div class="post-metadata">

**Author:** ![mij](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mij](https://discourse.gohugo.io/u/mij)\
**Post date:** [November 8, 2021, 12:06pm UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/6 "2021-11-08T12:06:49Z")

</div>

> You cannot call a shortcode from a shortcode.

Ok, this answers it.

The table you showed at the beginning pushed me to show code for shortcode and shortcode, as partials → shortcode showed “x”.

It’s a painful limitation if shortcodes can be used neither within shortcodes nor partials. I have other cases where I’d like to recycle graphs between blog posts.

Each graph is drown using [echarts](https://echarts.apache.org), for which my theme has a [echarts shortcode](https://ublogger.netlify.app/theme-documentation-extended-shortcodes/#6-echarts) available. That would basically require to repeat the charts in every post, and multiply edits whenever a change must be done.

Any workaround you could think of for that, which I can further look into?

Thanks.

@iaeiou That gets you to rebuild the raw HTML for infoblocks in the partial, if I understand you right. I’d like to avoid that if possible.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 8, 2021, 6:20pm UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/7 "2021-11-08T18:20:50Z")

</div>

First create a partial that does what you need. Then create a shortcode to wrap the partial. Here’s a silly example that:

- Scrambles the page’s title, month, and weekday
- Renders the desired number of words, in lowercase, in the color you specify

> **layouts/partials/scramble.html**
>
> ```auto
> {{ $page := .page }}
> {{ $count := .count }}
> {{ $color := .color }}
> {{ $dateWords := split ($page.Date.Format "January Monday") " " }}
> {{ $titleWords := split $page.Title " " }}
> {{ $shuffled := append $dateWords $titleWords | shuffle | first $count }}
> <span style="color: {{ $color }}">{{ delimit $shuffled " " | lower }}</span>
> 
> ```

  

> **layouts/shortcodes/scramble.html**
>
> ```auto
> {{ $msg := "The %q shortcode requires a %q parameter. See %s" }}
> {{ with $count := .Get "count" }}
> {{ with $color := $.Get "color" }}
> {{ partial "scramble" (dict "page" $.Page "count" $count "color" $color) }}
> {{ else }}
> {{ errorf $msg $.Name "color" $.Position }}
> {{ end }}
> {{ else }}
> {{ errorf $msg .Name "count" .Position }}
> {{ end }}
> 
> ```

  

To call the shortcode from markdown:

```auto
{{< scramble count=6 color=green >}}

```

To call the partial from a shortcode, template, or from another partial:

```auto
{{ partial "scramble" (dict "page" .Page "count" $count "color" $color) }}

```

Notes:

1. The shortcode has 10 lines of code. One line calls the partial, the rest is for data validation.
2. When calling the partial, the value of the “page” map element must be the .Page context. The syntax of the call will depend on the… context.

---

<div class="post-metadata">

**Author:** ![mij](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mij](https://discourse.gohugo.io/u/mij)\
**Post date:** [November 14, 2021, 7:57pm UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/8 "2021-11-14T19:57:46Z")

</div>

Thanks for the suggestion.

The problem I mentioned in the initial post persists – so it might be that we’re talking away from each other.

As soon as I prepare a file for the partial, even before I can create the shortcode for it, Hugo fails with error:

```auto
Process: loading templates: "/Users/myname/gitlab/mypath/layouts/partials/graph_cpi.html:1:1": parse failed: template: partials/graph_cpi.html:1: unexpected "<" in command

```

To avoid any unnecessary ping-pong, here’s the process I use:

1. I create file `layouts/partials/graph_cpi.html`
2. I fill the file with the graph code. This code contains the `{{< echarts >}}` shortcode provided by my theme (as I linked in my previous answer).
3. I save the file. At this point already, hugo fails with `Process: loading templates "[snip]: parse failed: template: partials/graph_cpi.html:1: unexpected "<" in command`
4. At this point, I would proceed to create a shortcode that wraps the partial – but given the error, I can no longer proceed.

Now, here’s the (minimized) code of that partial:

```auto
{{< echarts >}}
{
    "backgroundColor": "transparent",
    "title": {
        "text": "Inflation in Switzerland",
        "subtext": "Consumer Price Index [CHF], 1928 to 2018. Source: Swiss Federal Statistical Office."
    }
}
{{< /echarts >}}

```

What did I misunderstand of your proposal?

Using the echarts shortcode within the template is useful to avoid building myself the logic to load-once the respective JS dependency and graph loading code.

Thank you for the help

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [November 14, 2021, 8:21pm UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/9 "2021-11-14T20:21:13Z")

</div>

> [@mij](#):
>
> - I create file `layouts/partials/graph_cpi.html`
> - I fill the file with the graph code. This code contains the `{{< echarts >}}` shortcode

You can’t do that.

![image](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/3/31125e17a29930409d7ca462f6bb74868f8444a6.png)

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [November 16, 2021, 8:21pm UTC](https://discourse.gohugo.io/t/expand-shortcodes-or-partials-within-custom-shortcodes/35466/10 "2021-11-16T20:21:28Z")

</div>

Closed on November 16, 2021, 8:21pm UTC

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