Files
hugo/content/en/methods/page/HasShortcode.md
T
2025-03-05 04:50:06 -08:00

1.1 KiB

title, description, categories, keywords, params
title description categories keywords params
HasShortcode Reports whether the given shortcode is called by the given page.
functions_and_methods
related returnType signatures
bool
PAGE.HasShortcode NAME

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

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

The shortcode is simple:

{{< code file=layouts/shortcodes/plotly.html >}} {{ $id := printf "plotly-%02d" .Ordinal }}

<script> Plotly.newPlot(document.getElementById({{ $id }}), {{ .Inner | safeJS }}); </script> {{< /code >}}

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

{{< code file=layouts/baseof.html >}}

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