Debian Codegraph @

docs/content/en/methods/page/HasShortcode.md

raw
1 ---
2 title: HasShortcode
3 description: Reports whether the given shortcode is called by the given page.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: bool
9 signatures: [PAGE.HasShortcode NAME]
10 ---
11
12 By example, let's use [Plotly][] to render a chart:
13
14 ```md {file="content/example.md"}
15 {{</* plotly */>}}
16 {
17 "data": [
18 {
19 "x": ["giraffes", "orangutans", "monkeys"],
20 "y": [20, 14, 23],
21 "type": "bar"
22 }
23 ],
24 }
25 {{</* /plotly */>}}
26 ```
27
28 The shortcode is simple:
29
30 ```go-html-template {file="layouts/_shortcodes/plotly.html"}
31 {{ $id := printf "plotly-%02d" .Ordinal }}
32 <div id="{{ $id }}"></div>
33 <script>
34 Plotly.newPlot(document.getElementById({{ $id }}), {{ .Inner | safeJS }});
35 </script>
36 ```
37
38 Now we can selectively load the required JavaScript on pages that call the "plotly" shortcode:
39
40 ```go-html-template {file="layouts/baseof.html"}
41 <head>
42 ...
43 {{ if .HasShortcode "plotly" }}
44 <script src="https://cdn.plot.ly/plotly-2.28.0.min.js"></script>
45 {{ end }}
46 ...
47 </head>
48 ```
49
50 [Plotly]: https://plotly.com/javascript/