children: add parameter for container style #53

This commit is contained in:
Sören Weber 2021-08-25 14:45:01 +02:00
parent 15cfc82ddb
commit 1fda1d8692
No known key found for this signature in database
GPG key ID: 07D17FF580AE7589
2 changed files with 23 additions and 21 deletions

View file

@ -11,7 +11,8 @@ Use the children shortcode to list the child pages of a page and the further des
| Parameter | Default | Description |
|:--|:--|:--|
| page | _current_ | Specify the page name (section name) to display children for |
| style | "li" | Choose the style used to display descendants. It could be any HTML tag name |
| containerstyle | "ul" | Choose the style used to group all children. It could be any HTML tag name |
| style | "li" | Choose the style used to display each descendant. It could be any HTML tag name |
| showhidden | "false" | When true, child pages hidden from the menu will be displayed |
| description | "false" | Allows you to include a short text under each page in the list. When no description exists for the page, children shortcode takes the first 70 words of your content. [Read more info about summaries on gohugo.io](https://gohugo.io/content/summaries/) |
| depth | 1 | Enter a number to specify the depth of descendants to display. For example, if the value is 2, the shortcode will display 2 levels of child pages. **Tips:** set 999 to get all descendants |
@ -38,13 +39,13 @@ Use the children shortcode to list the child pages of a page and the further des
{{% children depth="3" showhidden="true" %}}
````go
{{%/* children style="h2" depth="3" description="true" */%}}
{{%/* children containerstyle="div" style="h2" depth="3" description="true" */%}}
````
{{% children style="h2" depth="3" description="true" %}}
{{% children containerstyle="div" style="h2" depth="3" description="true" %}}
````go
{{%/* children style="div" depth="999" */%}}
{{%/* children containerstyle="div" style="div" depth="999" */%}}
````
{{% children style="div" depth="999" %}}
{{% children containerstyle="div" style="div" depth="999" %}}

View file

@ -4,8 +4,9 @@
{{ $depth := .Get "depth" | default 1 }}
{{ $withDescription := .Get "description" | default false }}
{{ $sortTerm := .Get "sort" | default "Weight" }}
{{ $containerstyle := .Get "containerstyle" | default "ul" }}
<ul class="children children-{{$style}}">
{{(printf "<%s class=\"children children-%s\">" $containerstyle $style)|safeHTML}}
{{ .Scratch.Set "pages" .Page.Pages }}
{{if .Page.IsHome}}
@ -21,19 +22,19 @@
{{ $pages := (.Scratch.Get "pages") }}
{{if eq $sortTerm "Weight"}}
{{template "childs" dict "menu" $pages.ByWeight "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{template "childs" dict "menu" $pages.ByWeight "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{else if eq $sortTerm "Name"}}
{{template "childs" dict "menu" $pages.ByTitle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{template "childs" dict "menu" $pages.ByTitle "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{else if eq $sortTerm "PublishDate"}}
{{template "childs" dict "menu" $pages.ByPublishDate "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{template "childs" dict "menu" $pages.ByPublishDate "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{else if eq $sortTerm "Date"}}
{{template "childs" dict "menu" $pages.ByDate "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{template "childs" dict "menu" $pages.ByDate "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{else if eq $sortTerm "Length"}}
{{template "childs" dict "menu" $pages.ByLength "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{template "childs" dict "menu" $pages.ByLength "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{else}}
{{template "childs" dict "menu" $pages "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{template "childs" dict "menu" $pages "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
{{end}}
</ul>
{{(printf "</%s>" $containerstyle)|safeHTML}}
{{ define "childs" }}
{{ range .menu }}
@ -65,7 +66,7 @@
{{ if lt $.count $.depth}}
{{if eq $.style "li"}}
<ul>
{{(printf "<%s>" $.containerstyle)|safeHTML}}
{{end}}
{{ if .Sections}}
@ -77,21 +78,21 @@
{{ $pages := (.Scratch.Get "pages") }}
{{if eq $.sortTerm "Weight"}}
{{template "childs" dict "menu" $pages.ByWeight "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{template "childs" dict "menu" $pages.ByWeight "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{else if eq $.sortTerm "Name"}}
{{template "childs" dict "menu" $pages.ByTitle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{template "childs" dict "menu" $pages.ByTitle "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{else if eq $.sortTerm "PublishDate"}}
{{template "childs" dict "menu" $pages.ByPublishDate "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{template "childs" dict "menu" $pages.ByPublishDate "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{else if eq $.sortTerm "Date"}}
{{template "childs" dict "menu" $pages.ByDate "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{template "childs" dict "menu" $pages.ByDate "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{else if eq $.sortTerm "Length"}}
{{template "childs" dict "menu" $pages.ByLength "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{template "childs" dict "menu" $pages.ByLength "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{else}}
{{template "childs" dict "menu" $pages "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{template "childs" dict "menu" $pages "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
{{end}}
{{if eq $.style "li"}}
</ul>
{{(printf "</%s>" $.containerstyle)|safeHTML}}
{{end}}
{{end}}
{{end}}