Check if page is descendant of page with a specific template

I need to check if a page (no matter what depth it is), is the descendant of a page with a certain template.

I have used isDescendantOf() to check for a specific page before, but am not sure of the best way to check the template used through all ancestor pages.

Ultimately I need to grab a field value from the ancestor as well, to be used on its child. It needs to work something like this (psuedo code!):

if($page->isDescendantOf(/* Page with template foo */):
// Grab the value of the themeColour field from that ancestor
<?php endif ?>

If anyone has any pointers that would be great

i would propbably work in the other direction, something like

$themePage = $page->parents()->filterBy('template', 'yourtemplatename')->first();
$themeValue = $themePage->themeColor()->or('fallbackvalue');

I havent tried that out, you might need a little more in there to check the value exists etc before trying to use the value. The ->or() method should do that though i think.

Ah, I hadn’t thought about it that way. Thank you!

1 Like