hugo/docs/content/en/methods/page/HasShortcode.md
Bjørn Erik Pedersen 5fd1e74903
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
```
git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash
```

Closes #11925
2024-01-27 10:48:57 +01:00

1.1 KiB

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

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

{{< code file=contents/example.md lang=markdown >}} {{</* 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 }}

{{< /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" }} {{ end }} ... </head> {{< /code >}}