mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-22 21:42:50 +02:00
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
``` git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash ``` Closes #11925
This commit is contained in:
50
docs/content/en/methods/shortcode/Parent.md
Normal file
50
docs/content/en/methods/shortcode/Parent.md
Normal file
@@ -0,0 +1,50 @@
|
||||
---
|
||||
title: Parent
|
||||
description: Returns the parent shortcode context in nested shortcodes.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
related: []
|
||||
returnType: hugolib.ShortcodeWithPage
|
||||
signatures: [SHORTCODE.Parent]
|
||||
---
|
||||
|
||||
This is useful for inheritance of common shortcode parameters from the root.
|
||||
|
||||
In this contrived example, the "greeting" shortcode is the parent, and the "now" shortcode is child.
|
||||
|
||||
{{< code file=content/welcome.md lang=md >}}
|
||||
{{</* greeting dateFormat="Jan 2, 2006" */>}}
|
||||
Welcome. Today is {{</* now */>}}.
|
||||
{{</* /greeting */>}}
|
||||
{{< /code >}}
|
||||
|
||||
{{< code file=layouts/shortcodes/greeting.html >}}
|
||||
<div class="greeting">
|
||||
{{ trim .Inner "\r\n" | .Page.RenderString }}
|
||||
</div>
|
||||
{{< /code >}}
|
||||
|
||||
{{< code 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 -}}
|
||||
{{< /code >}}
|
||||
|
||||
The "now" shortcode formats the current time using:
|
||||
|
||||
1. The `dateFormat` parameter passed to the "now" shortcode, if present
|
||||
2. The `dateFormat` parameter passed to the "greeting" shortcode, if present
|
||||
3. The default layout string defined at the top of the shortcode
|
Reference in New Issue
Block a user