# HasShortcode
> 报告给定页面是否调用了指定的短代码。
- 官方英文原文：https://gohugo.io/methods/page/hasshortcode/
- 本页规范地址：https://hugozh.cn/methods/page/hasshortcode/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：PAGE.HasShortcode NAME
- 返回类型：bool
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
举个例子，我们用 [Plotly][] 渲染一张图表：

```md {file="content/example.md"}
{
  "data": [
    {
      "x": ["giraffes", "orangutans", "monkeys"],
      "y": [20, 14, 23],
      "type": "bar"
    }
  ],
}
```

这个短代码很简单：

```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>
```

现在我们可以只在调用 `plotly` 短代码的页面上按需加载所需的 JavaScript：

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

[Plotly]: https://plotly.com/javascript/

