IsAncestor
Reports whether PAGE1 is an ancestor of PAGE2.
- Syntax
- PAGE1.IsAncestor PAGE2
- Returns
- bool
Basic usage
With this content structure:
content/
├── auctions/
│ ├── 2023-11/
│ │ ├── _index.md
│ │ ├── auction-1.md
│ │ └── auction-2.md
│ ├── 2023-12/
│ │ ├── _index.md
│ │ ├── auction-3.md
│ │ └── auction-4.md
│ ├── _index.md
│ ├── bidding.md
│ └── payment.md
└── _index.mdWhen rendering the auctions page:
{{ with site.GetPage "/" }}
{{ $.IsAncestor . }} → false
{{ end }}
{{ with site.GetPage "/auctions" }}
{{ $.IsAncestor . }} → false
{{ end }}
{{ with site.GetPage "/auctions/2023-11" }}
{{ $.IsAncestor . }} → true
{{ end }}
{{ with site.GetPage "/auctions/2023-11/auction-2" }}
{{ $.IsAncestor . }} → true
{{ end }}In the examples above we are coding defensively using the with statement, returning nothing if the page does not exist. By adding an else clause we can do some error reporting:
{{ $path := "/auctions/2023-11" }}
{{ with site.GetPage $path }}
{{ $.IsAncestor . }} → true
{{ else }}
{{ errorf "Unable to find the section with path %s" $path }}
{{ end }}Understanding context
Inside of the with block, the context (the dot) is the section Page object, not the Page object passed into the template. If we were to use this syntax:
{{ with site.GetPage "/auctions" }}
{{ .IsAncestor . }} → true
{{ end }}The result would be wrong when rendering the auction-1 page because we are comparing the section page to itself. Use the $ to get the context passed into the template.
{{ with site.GetPage "/auctions" }}
{{ $.IsAncestor . }} → true
{{ end }}To write effective templates, you must understand how context works.
Last updated:
November 2, 2026
:
content: Prefer site function in code examples (3350b883b)
Improve this page