Hello
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!
Community guidelines
Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.
Hello
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 }}
@paul:
{{ 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 🙂
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 }}
@paul:
{{ page.parent.header.body_classes }}
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?
Log in to reply.
| Topic | Participants | Replies | Views | Activity |
|---|---|---|---|---|
| 2 | 70 | 5 hours ago | ||
| 1 | 45 | 9 hours ago | ||
| 0 | 43 | 22 hours ago | ||
| 6 | 341 | 4 days ago | ||
| 3 | 35 | 5 days ago |