# Parent
> 在嵌套短代码中返回父短代码的上下文。
- 官方英文原文：https://gohugo.io/methods/shortcode/parent/
- 本页规范地址：https://hugozh.cn/methods/shortcode/parent/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：SHORTCODE.Parent
- 返回类型：hugolib.ShortcodeWithPage
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
这对于从根级继承公共的短代码参数很有用。

在这个刻意构造的示例中，「greeting」短代码是父级，「now」短代码是子级。

```md {file="content/welcome.md"}
Welcome. Today is {{</* now */>}}.
```

```go-html-template {file="layouts/_shortcodes/greeting.html"}
<div class="greeting">
  {{ .Inner | strings.TrimSpace | .Page.RenderString }}
</div>
```

```go-html-template {file="layouts/_shortcodes/now.html"}
{{- $dateFormat := "January 2, 2006 15:04:05" }}

{{- with .Params }}
  {{- with .dateFormat }}
    {{- $dateFormat = . }}
  {{- end }}
{{- else }}
  {{- with .Parent.Params }}
    {{- with .dateFormat }}
      {{- $dateFormat = . }}
    {{- end }}
  {{- end }}
{{- end }}

{{- now | time.Format $dateFormat -}}
```

「now」短代码按下面的顺序格式化当前时间：

1. 传给「now」短代码的 `dateFormat` 参数（如果存在）
1. 传给「greeting」短代码的 `dateFormat` 参数（如果存在）
1. 短代码顶部定义的默认布局字符串

