| 1 |
--- |
| 2 |
title: Parent |
| 3 |
description: Returns the parent shortcode context in nested shortcodes. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: hugolib.ShortcodeWithPage |
| 9 |
signatures: [SHORTCODE.Parent] |
| 10 |
--- |
| 11 |
|
| 12 |
This is useful for inheritance of common shortcode arguments from the root. |
| 13 |
|
| 14 |
In this contrived example, the "greeting" shortcode is the parent, and the "now" shortcode is child. |
| 15 |
|
| 16 |
```md {file="content/welcome.md"} |
| 17 |
{{</* greeting dateFormat="Jan 2, 2006" */>}} |
| 18 |
Welcome. Today is {{</* now */>}}. |
| 19 |
{{</* /greeting */>}} |
| 20 |
``` |
| 21 |
|
| 22 |
```go-html-template {file="layouts/_shortcodes/greeting.html"} |
| 23 |
<div class="greeting"> |
| 24 |
{{ .Inner | strings.TrimSpace | .Page.RenderString }} |
| 25 |
</div> |
| 26 |
``` |
| 27 |
|
| 28 |
```go-html-template {file="layouts/_shortcodes/now.html"} |
| 29 |
{{- $dateFormat := "January 2, 2006 15:04:05" }} |
| 30 |
|
| 31 |
{{- with .Params }} |
| 32 |
{{- with .dateFormat }} |
| 33 |
{{- $dateFormat = . }} |
| 34 |
{{- end }} |
| 35 |
{{- else }} |
| 36 |
{{- with .Parent.Params }} |
| 37 |
{{- with .dateFormat }} |
| 38 |
{{- $dateFormat = . }} |
| 39 |
{{- end }} |
| 40 |
{{- end }} |
| 41 |
{{- end }} |
| 42 |
|
| 43 |
{{- now | time.Format $dateFormat -}} |
| 44 |
``` |
| 45 |
|
| 46 |
The "now" shortcode formats the current time using: |
| 47 |
|
| 48 |
1. The `dateFormat` argument passed to the "now" shortcode, if present |
| 49 |
1. The `dateFormat` argument passed to the "greeting" shortcode, if present |
| 50 |
1. The default layout string defined at the top of the shortcode |