As you can see I am parsing a hex colour value to global stylesheet variables. My site page structure uses the ‘department’ template as parent pages and the ‘blocks’ template as a child template. The above works in an instance where the blocks child page is only 1 layer deep, but if it has another child then the parent it calls is just the block template, and I’m only storing the value in the department template.
Is there a function that allows me to pull a variable from the last instance of a template in a page url. So for example,
<?php if ($page->template() == 'blocks'): ?>
// get primaryColour field from last parent that uses the 'department' template
<?php endif ?>
But I’m struggling to use this with some login, which i’m sure is very rudamentry
<?= $parentDepartment = $page->parents()->findBy("intendedTemplate", "department");?>
<?php if ($parentDepartment->title()->isEmpty()): ?>
note: there is no department template in this url path
<?php endif ?>
To understand this, it is important to learn that Kirby is based on objects. A page, a pages collection, a field etc. are all objects and these objects are instances of their. class. Each class comes with a set of methods/properties. Put the other way round, when calling such a method, this in turn of course requires an object of this class. This means you cannot call such a method on anything else (not a string, not an array, not another type of object and of course also not on null). Further introduction: A brief intro to object oriented programming in PHP | Kirby CMS
You can use an or statement
<?php if $page->intendedTemplate()->name() === 'department' || $parentPage = $page->parents()->findBy('intendedTemplate', 'department')): ?>
<!-- do stuff -->
<?php endif ?>