# Parsing data from parents based on template

**URL:** <https://forum.getkirby.com/t/parsing-data-from-parents-based-on-template/27828>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 11, 2023, 12:43pm UTC](https://forum.getkirby.com/t/parsing-data-from-parents-based-on-template/27828 "2023-02-11T12:43:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dilby](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dilby/32/3221_2.png) [@dilby](https://forum.getkirby.com/u/dilby)\
**Post date:** [February 11, 2023, 12:43pm UTC](https://forum.getkirby.com/t/parsing-data-from-parents-based-on-template/27828/1 "2023-02-11T12:43:02Z")

</div>

Hi all - I have an unique site setup where I am storing a variable in a page blueprint and need to fetch this in subpages.

**department.yml**

```auto
fields:
      primaryColour:
        type: text

```

**header.php snippet**

```auto
<style>
:root {
<?php if ($page->template() == 'department'): ?>
  --primary: #<?= $page->primaryColour() ?>;
<?php elseif ($page->template() == 'blocks'): ?>
  --primary: #<?= $page->parent()->primaryColour() ?>;
<?php else: ?>
  --primary: #53A9B2;
<?php endif ?>
}
</style>

```

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,

```auto
<?php if ($page->template() == 'blocks'): ?>
// get primaryColour field from last parent that uses the 'department' template
<?php endif ?>

```

Thanks so much

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 11, 2023, 12:54pm UTC](https://forum.getkirby.com/t/parsing-data-from-parents-based-on-template/27828/2 "2023-02-11T12:54:12Z")

</div>

```auto
$page->parents()->last()

```

gives you the “root” ancestor.

---

<div class="post-metadata">

**Author:** ![dilby](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dilby/32/3221_2.png) [@dilby](https://forum.getkirby.com/u/dilby)\
**Post date:** [February 11, 2023, 1:54pm UTC](https://forum.getkirby.com/t/parsing-data-from-parents-based-on-template/27828/3 "2023-02-11T13:54:03Z")

</div>

Thanks for the reply. So I’ve got that working when on a page that has a parent with the chosen template in the path:

```auto
<?= $parentDepartment = $page->parents()->findBy("intendedTemplate", "department");?>
<?= $parentDepartment->title() ?>

```

But I’m struggling to use this with some login, which i’m sure is very rudamentry

```auto
<?= $parentDepartment = $page->parents()->findBy("intendedTemplate", "department");?>

<?php if ($parentDepartment->title()->isEmpty()): ?>

note: there is no department template in this url path

<?php endif ?>

```

Any help appreciated, thank you

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 11, 2023, 1:57pm UTC](https://forum.getkirby.com/t/parsing-data-from-parents-based-on-template/27828/4 "2023-02-11T13:57:00Z")

</div>

> [@dilby](#):
>
> `<?php if ($parentDepartment->title()->isEmpty()): ?>`

What are you trying to check here? This will result in a PHP error when there is no parentdepartment page (calling title on null)

If you want to check if the page exists, then that would be

```auto
if ($parentDepartment) {
  // exists
}

```

---

<div class="post-metadata">

**Author:** ![dilby](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dilby/32/3221_2.png) [@dilby](https://forum.getkirby.com/u/dilby)\
**Post date:** [February 11, 2023, 2:13pm UTC](https://forum.getkirby.com/t/parsing-data-from-parents-based-on-template/27828/5 "2023-02-11T14:13:15Z")

</div>

Thanks so much, I didn’t realise that the page didn’t exist it would error, I thought it would just return as empty. That’s working now.

However another cheeky question, is there a way to make the below also cover the current page.

So specifically, find intendedTemplate within parents AND current page.

`<?= $parentDepartment = $page->parents()->findBy("intendedTemplate", "department");?>`

Thanks again for the invaluable help

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 11, 2023, 2:26pm UTC](https://forum.getkirby.com/t/parsing-data-from-parents-based-on-template/27828/6 "2023-02-11T14:26:03Z")

</div>

> [@dilby](#):
>
> I didn’t realise that the page didn’t exist it would error, I thought it would just return as empty. That’s working now.

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](https://getkirby.com/docs/cookbook/templating/understanding-oop)

> [@dilby](#):
>
> So specifically, find intendedTemplate within parents AND current page.

You can use an or statement

```auto
<?php if $page->intendedTemplate()->name() === 'department' || $parentPage = $page->parents()->findBy('intendedTemplate', 'department')): ?>

<!-- do stuff -->
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![dilby](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/dilby/32/3221_2.png) [@dilby](https://forum.getkirby.com/u/dilby)\
**Post date:** [February 11, 2023, 2:41pm UTC](https://forum.getkirby.com/t/parsing-data-from-parents-based-on-template/27828/7 "2023-02-11T14:41:46Z")

</div>

That makes sense, so much to learn and thanks so much for that it’s so helpful!
