Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add change class on body for different pages in nunjucks?

I'm using Mozilla Nunjucks templating system for a static website which has multiple pages. For some purpose, I want to add a class to body tag for each different page

About us page > <body class="about">

Home > <body class="home">

I can do this very easily if body tag I keep inside every page but I want to keep body tag inside my template which I would be using for all pages.

I want to change classes conditionally.

like image 327
Jitendra Vyas Avatar asked Dec 18 '22 23:12

Jitendra Vyas


1 Answers

  1. Set variable on your page. On your page.njk: {% set bodyClass = "pageClass" %}
  2. Call bodyClass on layout. <body{% if bodyClass %} class="{{ bodyClass }}"{% endif %}>
like image 142
Nizamil Putra Avatar answered May 08 '23 12:05

Nizamil Putra