Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Apply body class from parent page?

Started by Matthew Appleton 8 years ago · 8 replies · 1555 views
8 years ago

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!

8 years ago

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

8 years ago

@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...

11|519x500

8 years ago

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

8 years ago

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...

TWIG
{% block body_classes %}

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

{{ class }}

{% endif %}

{% endblock %}
8 years ago

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

{{ page.parent.header.body_classes }}

👍 1
8 years ago

@paul:
{{ page.parent.header.body_classes }}

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

8 years ago

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?

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 3 days ago
2 70 5 hours ago
General · by pamtbaau, 10 hours ago
1 45 9 hours ago
General · by Andy Miller, 22 hours ago
0 43 22 hours ago
General · by Marcel, 12 months ago
6 341 4 days ago
General · by Duc , 5 days ago
3 35 5 days ago