mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
Merge commit '5be51ac3db225d5df501ed1fa1499c41d97dbf65'
This commit is contained in:
@@ -3,17 +3,17 @@ title: HasShortcode
|
||||
description: Reports whether the given shortcode is called by the given page.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
related: []
|
||||
returnType: bool
|
||||
signatures: [PAGE.HasShortcode NAME]
|
||||
params:
|
||||
functions_and_methods:
|
||||
returnType: bool
|
||||
signatures: [PAGE.HasShortcode NAME]
|
||||
---
|
||||
|
||||
By example, let's use [Plotly] to render a chart:
|
||||
|
||||
[Plotly]: https://plotly.com/javascript/
|
||||
|
||||
{{< code file=contents/example.md lang=markdown >}}
|
||||
```text {file="content/example.md"}
|
||||
{{</* plotly */>}}
|
||||
{
|
||||
"data": [
|
||||
@@ -25,21 +25,21 @@ By example, let's use [Plotly] to render a chart:
|
||||
],
|
||||
}
|
||||
{{</* /plotly */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
The shortcode is simple:
|
||||
|
||||
{{< code file=layouts/shortcodes/plotly.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/plotly.html"}
|
||||
{{ $id := printf "plotly-%02d" .Ordinal }}
|
||||
<div id="{{ $id }}"></div>
|
||||
<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 >}}
|
||||
```go-html-template {file="layouts/_default/baseof.html"}
|
||||
<head>
|
||||
...
|
||||
{{ if .HasShortcode "plotly" }}
|
||||
@@ -47,4 +47,4 @@ Now we can selectively load the required JavaScript on pages that call the "plot
|
||||
{{ end }}
|
||||
...
|
||||
</head>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
Reference in New Issue
Block a user