# RenderShortcodes
> 返回给定页面的内容，其中所有短代码都已渲染，同时保留其周围的标记。
- 官方英文原文：https://gohugo.io/methods/page/rendershortcodes/
- 本页规范地址：https://hugozh.cn/methods/page/rendershortcodes/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：PAGE.RenderShortcodes
- 返回类型：template.HTML
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
在_短代码_模板中使用该方法，可以从多个内容文件组合出一个页面，同时为脚注和目录保留全局上下文。

例如：

```go-html-template {file="layouts/_shortcodes/include.html" copy=true}
{{ with .Get 0 }}
  {{ with $.Page.GetPage . }}
    {{- .RenderShortcodes }}
  {{ else }}
    {{ errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position }}
  {{ end }}
{{ else }}
  {{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }}
{{ end }}
```

然后在你的 Markdown 中调用该短代码：

```md {file="content/about.md"}
```

每个被包含的 Markdown 文件都可以包含对其他短代码的调用。

## 短代码记法

在上例中，理解调用短代码时所用两种定界符的区别很重要：

- `{{</* myshortcode */>}}` 告诉 Hugo，渲染后的短代码不需要进一步处理。例如，短代码的内容是 HTML。
- `{{%/* myshortcode */%}}` 告诉 Hugo，渲染后的短代码需要进一步处理。例如，短代码的内容是 Markdown。

对于上面描述的 “include” 短代码，请使用后者。

## 进一步说明

要理解 `RenderShortcodes` 方法返回什么，请看这个内容文件

```md {file="content/about.md"}
+++
title = 'About'
date = 2023-10-07T12:28:33-07:00
+++


An *emphasized* word.
```

配合这段模板代码：

```go-html-template
{{ $p := site.GetPage "/about" }}
{{ $p.RenderShortcodes }}
```

Hugo 渲染出：;

```html
https://example.org/privacy/

An *emphasized* word.
```

注意内容文件中的短代码被渲染了，而周围的 Markdown 被保留了下来。

## 限制

`.RenderShortcodes` 的主要用途是包含 Markdown 内容。如果你在 Markdown 中的 `HTML` 块里使用 `.RenderShortcodes`，会收到类似这样的警告：

```text
WARN .RenderShortcodes detected inside HTML block in "/content/mypost.md"; this may not be what you intended ...
```

如果这确实是你想要的效果，可以关闭上述警告。

