---
title: 'Access parent page property recursively'
url: 'https://getgrav.org/forum/themes-styling/access-parent-page-property-recursively-t7053'
markdown: 'https://getgrav.org/forum/themes-styling/access-parent-page-property-recursively-t7053.md'
date: '2026-06-21'
description: 'Hi there! I am currently creating a custom theme and I add a color property on my pages. I want, in templates, get the color property for the page and if not'
---

[Themes & Styling](https://getgrav.org/forum/themes-styling)

#   Access parent page property recursively 

 Solved by Kubenexion [View solution](https://getgrav.org/forum/themes-styling/access-parent-page-property-recursively-t7053#post-19458)

  Started by Kubenexion 9 years ago · 2 replies · 1533 views    

 [9 years ago](https://getgrav.org/forum/themes-styling/access-parent-page-property-recursively-t7053#post-19440 "Permalink") 

Hi there!

I am currently creating a custom theme and I add a color property on my pages.

I want, in templates, get the color property for the page and if not defined the parent page color property and if not defined the grand-parent page color property...

Is there a function to get a property recursively?

I tried to make one:

```php
    public function onTwigInitialized()
    {
        $this->grav['twig']->twig()->addFunction(
            new \Twig_SimpleFunction('get_color', [$this, 'getColor'])
        );
    }

    function getColor($page)
    {
        do {
            $color = $page->header->color;
            $page = $page->parent;
        } while(!$color && $page);

        return $color;
    }
```

It defines a custom twig function `get_color` with a `page` parameter, but `$page->header` is a protected field...

 [9 years ago](https://getgrav.org/forum/themes-styling/access-parent-page-property-recursively-t7053#post-19458 "Permalink")  Solution 

After reading the API and dumping some vars I found a solution:

```php
    public function onTwigInitialized()
    {
        $this->grav['twig']->twig()->addFunction(
            new \Twig_SimpleFunction('recursive_header_var', [$this, 'getRecursiveHeaderVar'])
        );
    }

    function getRecursiveHeaderVar($varName)
    {
        $page = $this->grav["page"];
        do {
            $value = property_exists($page->header(), $varName) ? $page->header()->$varName : null;
            $page = $page->parent();
        } while ($value === null && $page->header());

        return $value;
    }
```

Now I can do `{{ recursive_header_var('color') }}` and get the color variable in the current page header or in the parent if not defined 😀

 👍 3 

 [9 years ago](https://getgrav.org/forum/themes-styling/access-parent-page-property-recursively-t7053#post-19495 "Permalink") 

Nice solution, thanks for sharing.

[Log in](https://getgrav.org/login?return=%2Fforum%2Fthemes-styling%2Faccess-parent-page-property-recursively-t7053) to reply.

###  Suggested topics

 | Topic | Participants | Replies | Views | Activity |
|---|---|---|---|---|
| [Grav2 with Quark2 – All pages in the hamburger-menu of a one-pager](https://getgrav.org/forum/themes-styling/grav2-with-quark2-all-pages-in-the-hamburger-menu-of-a-one-pager-t9485)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by AH, 2 weeks ago |  | 1 | 199 | [10 hours ago](https://getgrav.org/forum/themes-styling/grav2-with-quark2-all-pages-in-the-hamburger-menu-of-a-one-pager-t9485 "by AH") |
| [\[quark\] mobile menu as the modular page menu](https://getgrav.org/forum/themes-styling/quark-mobile-menu-as-the-modular-page-menu-t1132)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by Bruno, 4 years ago |  | 6 | 1350 | [10 hours ago](https://getgrav.org/forum/themes-styling/quark-mobile-menu-as-the-modular-page-menu-t1132 "by AH") |
| [Modular Page Background Image Display](https://getgrav.org/forum/themes-styling/modular-page-background-image-display-t9486)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by TomW, 2 weeks ago |  | 2 | 203 | [2 days ago](https://getgrav.org/forum/themes-styling/modular-page-background-image-display-t9486 "by TomW") |
| [Inheriting a Theme does not quite work correctly](https://getgrav.org/forum/themes-styling/inheriting-a-theme-does-not-quite-work-correctly-t9501)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by themetman, 3 days ago |  | 0 | 56 | [3 days ago](https://getgrav.org/forum/themes-styling/inheriting-a-theme-does-not-quite-work-correctly-t9501 "by themetman") |
| [Is it a way to get the "content - summary" in a twig template ?](https://getgrav.org/forum/themes-styling/is-it-a-way-to-get-the-content-summary-in-a-twig-template-t9431)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by FrViPofm, 2 months ago |  | 4 | 668 | [1 month ago](https://getgrav.org/forum/themes-styling/is-it-a-way-to-get-the-content-summary-in-a-twig-template-t9431 "by Paul Hibbitts") |
