HUGO
News Docs Themes Community GitHub

HasShortcode

Reports whether the given shortcode is called by the given page.

Syntax

PAGE.HasShortcode NAME

Returns

bool

By example, let’s use Plotly to render a chart:

contents/example.md
{{< plotly >}}
{
  "data": [
    {
      "x": ["giraffes", "orangutans", "monkeys"],
      "y": [20, 14, 23],
      "type": "bar"
    }
  ],
}
{{< /plotly >}}

The shortcode is simple:

layouts/shortcodes/plotly.html
{{ $id := printf "plotly-%02d" .Ordinal }}
<div id="{{ $id }}"></div>
<script>
  Plotly.newPlot(document.getElementById({{ $id }}), {{ .Inner | safeJS }});
</script>

Now we can selectively load the required JavaScript on pages that call the “plotly” shortcode:

layouts/baseof.html
<head>
  ...
  {{ if .HasShortcode "plotly" }}
    <script src="https://cdn.plot.ly/plotly-2.28.0.min.js"></script>
  {{ end }}
  ...
</head>