Apply body class from parent page?


I would like a child page to inherit / apply a body class being set in the parent template with this snippet:

{% block body_classes %}{{ body_classes }}{% endblock %}

{{ body_classes|parent }} doesn’t work!!

Any clues much appreciated!

Having a look at ‘parent’ and ‘use’ in Twig, but cannot get these to behave so far…

you could use {{ page.parent.body_classes }}

thanks Paul — that isnt working however… if I demo the page structure it might help? Basically I have an index page (project-index.html.twig) that has a collection of children pages (project-page.html.twig)

I am assigning the .archive class to the index page (a one-time operation), and I would love it if all the children of this index page could inherit this page’s body_classes…

just wondering if there is another way to force child classes using frontmatter?

OMG! managed to find a solution :slight_smile:

Using page.header, so a bit of a workaround and there is probably another way, but if I assign the ‘archive’ to my index page and then put this if statement into the child page template I get the right result…

{% block body_classes %}

{% set class = 'archive' %}
{% if page.parent.header.header_var == 'archive' %}

{{ class }}

{% endif %}

{% endblock %}

Glad you found it! I made an error indeed, correct twig was

{{ page.parent.header.body_classes }}

1 Like

That works! More elegant I think than the workaround I had, many thanks!!

In terms of jquery Add class of parent tag but if parent has some class then what kind of use of parent tag in body class?