Hi all,
I’m trying to modify the children shortcode from Learn/docdock to filter on sections under a particular level. For example, I have the following:
content
└── animals
├── _index.html
├── cat
│ ├── _index.html
│ ├── persian
│ │ └── _index.html
│ └── tabby
│ └── _index.htm
└── dog
├── _index.html
├── husky
│ └── _index.html
└── poodle
└── _index.html
And I want to add a short code to content/animals/_index.html
to filter and only process “dog” pages for instance. If I reference .Section
for each page in a range I get animals
which I expect.
When I try to use .CurrentSection
, which I interpret as the current section for the page being processed, it returns a page object for that page. For the same page, .Kind
returns “section”, so I’m stumped as to how to get the current section for “dog”, and then when I process those sub pages get “husky” and “poodle”.
I’m pretty sure there is something fundamental I’m missing, so any insights for how to get the current section would be appreciated!
Hi there,
It would be easier to help you if we were able to replicate your issue. Do you have your code somewhere we can have a look at?
See: Requesting Help
Sure thing. Here’s the abbreviated shortcode I want to drop onto a page and have it filter (not implemented) on the child pages based on their current section:
{{% sectiontest depth="2" %}}
Here’s the sectiontest.html
code:
{{ $depth := .Get "depth" | default 1 }}
<ul>
<!-- For testing, not checking for IsHome -->
{{ .Scratch.Set "pages" (.Page.Pages | union .Page.Sections) }}
{{ $pages := (.Scratch.Get "pages") }}
{{template "childs" dict "menu" $pages.ByWeight "count" 1 "depth" $depth "pages" .Site.Pages }}
</ul>
{{ define "childs" }}
<!-- "menu" contains the pages from location where shortcode is included -->
{{ range .menu }}
<!-- I want to filter based on the next child section below. Reference to .CurrentSection returns full page path -->
<p><a href="{{.RelPermalink}}" >{{ .Title }}</a> - Section: {{.CurrentSection}}</p>
<p>{{.Summary}}</p>
<!-- Continue down until depth -->
{{ if lt $.count $.depth}}
{{if eq $.style "li"}}
<ul>
{{end}}
{{ if .Sections}}
{{ .Scratch.Set "pages" (.Pages | union .Sections) }}
{{else}}
{{ .Scratch.Set "pages" .Pages }}
{{end}}
{{ $pages := (.Scratch.Get "pages") }}
{{template "childs" dict "menu" $pages.ByWeight "count" (add $.count 1) "depth" $.depth "pages" $.pages }}
{{if eq $.style "li"}}
</ul>
{{end}}
{{end}}
{{end}}
<!-- shortcode end -->
{{end}}
Just to follow up. I can get the string value I want by calculating the string value of .CurrentSection
with this:
{{ $elements := index (split .CurrentSection /) }}
{{ $section := index $elements (sub (len $elements) 2) }}
If .CurrentSection
contains Page(/animals/dog/_index.md)
, I will get dog
. In reading the docs, I would think that’s the value .CurrentSection
should return. Is the string value what is expected for that Section variable.
{{ .CurrentSection.Title }}
When troubleshooting, you can use this approach to determine type:
{{ printf "%T" .CurrentSection }}<br>
{{ printf "%T" .CurrentSection.Title }}<br>
2 Likes
.Section
returns the folder name off of the content/
folder, while .CurrentSection.Title
returns the title, which is the same for .Title
? It may be I don’t understand some of the terminology of returned section values and there are other aspects of Hugo where it is relevant?
In my case I have a working solution by splitting .CurrentSection
which returns the folder path.
I’m starting to grok how the variables and methods are listed. When I read this for .CurrentSection
:
The page’s current section. The value can be the page itself if it is a section or the homepage.
then I don’t see the difference between .Page
and .CurrentSection
. Unless it’s only when processing the homepage?
content
├── animals
│ ├── cat
│ │ ├── persian
│ │ │ └── index.md (1) (no underscore in file name)
│ │ └── _index.md (2)
│ └── _index.md (3)
└── _index.md (4)
1) content/animals/cat/persian/index.md (kind: page)
.Title = Persian
.CurrentSection.Title = Cats
2) content/animals/cat/_index.md (kind: section)
.Title = Cats
.CurrentSection.Title = Cats
3) content/animals/_index.md (kind: section)
.Title = Animals
.CurrentSection.Title = Animals
4) content/_index.md (kind: section)
.Title = My Site
.CurrentSection.Title = My Site
Note that cat was pluralized to cats when displaying the title. To disable, set pluralizeListTitles = false
in config.toml.
Ah, now this makes sense. Value and pluralization set based on page type (leaf/branch), and that derives the section based on title. So I should be matching on actual frontmatter title.