Testing for nested section

I must be missing something simple but I didn’t see it looking through docs and here.

I creating schema.org json+ld for my site and want to use different microdata depending on the page location in a nested section, but I can’t figure out how to elegantly test this.

GIven a simple example:

content/
├── about/
   ├── _index.md
   ├── employment/
      ├── _index.md
      ├── job.md

How can I test when job.md is in the section employment?
If it were a top level section check a simple {{ if eq .Section "about" }} would work for instance. Is there a similar approach for checking if the page is under the employment nested section?

From job.md, the .CurrentSection method gives you a page reference to content/about/employment/_index.md.

So you can do:

.CurrentSection.Title
.CurrentSection.RelPermalink
etc.
2 Likes

That makes sense. I guess I was thinking there might be something similar to .Section to avoid any fragility in the title change of the employment list page. :+1:

Once you have the page reference you can get to .File, so…

{{ .CurrentSection.File.ContentBaseName }}
1 Like

Aha! Now it’s something new and shiny to play with. Thank you!

If you go this route, you’ll need to code defensively for section pages that aren’t backed by a file.

{{ $foo := "" }}
{{ with .CurrentSection }}
  {{ with .File }}
    {{ $foo = .ContentBaseName }}
  {{ end }}
{{ end }}

{{ $foo }}
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.