How to know if a page belongs to a sub-section?

In my template {{ .Section }} returns the parent section name, but my page belongs to a nested section

section/nested-section/test-page.html

In my template, how do I get the following:

  1. “nested-section” from test-page.html
  2. If a page belongs to section level 1 or 2. Basically if my page is two or one level deep

Thank you!

Without directly answering your quesition, every page has a .Parent, .CurrentSection, .IsAncestor and .IsParent and .Sections

2 Likes

Without directly answering your quesition, every page has a .Parent, .CurrentSection, .IsAncestor and .IsParent and .Sections

None of these options carries the value I’m looking for.

I’ve got a catalog of products. The parent section is called “catalog” and the nested section is called “portal”. There I have one file for each product in that category, so: “/content/catalog/portal/product-1.html, /content/catalog/portal/product-2.html, etc.”

I have the same template for any sub-section in catalog, i.e. “/layout/catalog/single.html”. There I’d like have the name of the current section I’m in, i.e. “portal”.

I looked everywhere in the docs, tried debugging the object “.”, “.Page”, “.Section”, “.CurrentSection”, etc. I can’t find this information anywhere. The best I could find is, by printing the object “.” there’s a unexported key called “targetPathDescriptor”. There I have an exported key called “Sections” that is a slice of strings looking like this string[] {"catalog", "portal"}. But since it’s under an unexported key I can’t have it.

How do I do it?

3 Likes