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?

1 Like

Hi, it’s not obvious but documented here

{{ if .HasShortcode "name_of_shortcode" }}
2 Likes

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

layouts/shortcodes/my-shortcode.html

{{ .Page.Store.Set "email" "john@example.org" }}

layouts/_default/baseof.html

{{ .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
  • Len
  • Plain
  • PlainWords
  • ReadingTime
  • Summary (regardless of how you define it)
  • Truncated
  • WordCount

Example baseof.html

<head>
  ...
  {{ $noop := .WordCount }}
  {{ .Store.Get "email" }}
</head>
6 Likes

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.

Shortcode:

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

{{.Inner | markdownify }}

</p>

Then I went under the content folder and used this shortcode in the hugo.md 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?

2 Likes

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.

hugo.md 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

layouts/shortcodes/alert.html

{{- $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>

layouts/partials/head.html

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

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.