Question about intendedTemplate()

I have a ‘resume’ page with three different subpage types (‘skill’, ‘role’, and ‘education’). These subpages are never used directly—so they don’t have templates of their own, they are only used to build the parts of the ‘resume’ page. I’m trying to use the intendedTemplate() function to identify each of the subpage types, but it never seems to work. This is the relevant code in the ‘resume.php’ template file:

$subpage = $page->children()->visible()
...
if($subpage->intendedTemplate() == 'skill'):
foreach($subpage as $skill):
...
endforeach
endif

This unfortunately doesn’t result in any children being used. It’s not generating any PHP errors, it just doesn’t trigger. If someone could explain intendedTemplate a little or point out what’s wrong with the above code, I’d really appreciate the help.

You’re calling intendedTemplate on a collection of pages. That’s not going to work.
You need to filter your subpage collection by intended template instead of using the if.

$subpage = $page->children()->visible();
$skills = $subpage->filterBy('intendedTemplate', 'skill');
foreach ($skills as $skill):
...
endforeach;

Otherwise change the if statement to be inside the foreach, and use $skill->intendedTemplate()

1 Like

$subpage is a collection, not a single page, so you can’t use the method intendedTemplate(). You could filter by template, though:

$subpage = $page->children()->visible()->filterBy('intendedTemplate', 'skill');

Note, that your code above is also missing a semicolon at the end of the $subpage definition.

@Thiousi was faster.

2 Likes

Ah! You guys rock! Thanks so much!

2 Likes