Detecting the use of shortcodes

I created a shortcode in the style of Frequently Asked Questions. When this shortcode is used, I want to add schema data to the <head> section. Is there any way to do this?

Hi, it’s not obvious but documented here

{{ if .HasShortcode "name_of_shortcode" }}

Thanks, but is it possible to get data from shortocdes? I mean, if there are shortcodes named “email”, take the name “mail” entered in this shortcode.

1 Like


{{ .Page.Store.Set "email" "" }}


{{ .Store.Get "email" }}

If I understand the rendering order correctly, shortcodes are lazy loaded, so I believe there is no guarantee that the .Store value will be available at the top of your baseof template until .Content has been rendered.

To make sure that it is available, force .Content rendering before retrieving the .Store value. You can use any of the following to force .Content rendering:

  • Content
  • FuzzyWordCount
  • Plain
  • PlainWords
  • ReadingTime
  • Summary (regardless of whether or not it is defined in front matter)
  • TableOfContents
  • Truncated
  • WordCount

Example baseof.html

  {{ $noop := .WordCount }}
  {{ .Store.Get "email" }}

Thank you for your answer but unfortunately it doesn’t work for what I want. Let me explain you with an example.

I created my shortcode under layouts/shortcodes/alert.html.


<p style="color: {{ .Get "color" }}">

{{.Inner | markdownify }}


Then I went under the content folder and used this shortcode in the file as follows.

{{< alert color="green" >}}

Hugo <3

{{< /alert >}}

So far so good. The real problem starts after this.

I want to get the {{ .Get "color" }} variable in the head.html file automatically when the alert.html shortcode is used.

{{ if .HasShortcode "alert" }}

// code

{{ end }}

I can check if it is used with the code above, but I don’t know how to get the variable {{ .Get "color" }} automatically. What I need is to get the variable here automatically.

I couldn’t find such a feature in hugo in the documentation pages, it doesn’t exist automatically I will report it on Github issues. I would be very grateful if you could answer me.

1 Like

What if the alert shortcode is used twice on the same page, each with a different color?


he has to do the same process twice. Let me explain it like this.

Let’s say we have managed to get the variable used in the shortcode and we want to use it 2 times. I was planning the output to be like this.

I put the h1 code between head.html as an example, normally I will use this process for seo schema data. file:

{{< alert color="red" >}}

Hugo <3

{{< /alert >}}

{{< alert color="yellow" >}}

jmooring <3

{{< /alert >}}

head.html output:

<h1 style="color:red">Hugo <3</h1>

<h1 style="color:yellow">jmooring <3</h1>
1 Like


{{- $color := .Get "color" }}
{{- $content := .Inner | markdownify }}
{{- .Page.Store.SetInMap "alerts" (printf "alert_%d" .Ordinal) (dict "color" $color "content" $content) }}
<p style="color: {{ $color }}">{{ $content }}</p>


{{- $noop := .Content }}
{{- range .Store.GetSortedMapValues "alerts" }}
<h1 style="color:{{ .color }}">{{ .content }}</h1>
{{- end }}

Thank you very much for solving a very big problem.

1 Like

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