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

Twig if statement to select page template?

Started by Muut Archive 10 years ago · 6 replies · 1360 views
10 years ago

Is there a way to select a page template from an "if" statement? I would image there is, but I cannot find it. haha. Something like:

TXT

I'm trying to add a body class to my modular page without having to rely on doing it through the front-matter.

Here's what I have in my base.html.twig:

TXT

I read in documentation how to do stuff like that from the front matter, but not from the actual page template. Doing it though the blueprint would also be an option if that's possible.

10 years ago

Very close! You can do something like:

TWIG
<body class="{% if page.template == modular %} modular-page{% endif %}">

You can add the body_classes block too, but it's not required for this example.

10 years ago

Ahhh. SO. freaking. close. I must have tried a dozen different strings. haha

Thanks for the help!

10 years ago

Ok, looks like I may need to add the body_class block in there because it's not showing the new class in the html. I tried putting that if statement inside of the current

TXT

but it didn't print. I promise, I'm going to figure out this syntax eventually. haha

10 years ago

Need that {% endblock %} on the end to close the block.

10 years ago

Writing like this though, isn't showing the added body class on the front end.

<body class="{% block body_classes %},{{ page.header.body_classes }},{% if page.template == modular %} modular-page {% endif %},{% endblock %}">

10 years ago

Sorry, can't figure out how to post code from my iPad.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1362 9 years ago
Archive · by Muut Archive, 9 years ago
2 939 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2959 9 years ago
Archive · by Muut Archive, 9 years ago
3 1123 9 years ago