Search code examples
javascriptmacrosnunjucks

nunjucks macro to change a class


What would be the correct way to use macros in nunjucks to change the class in an include?

For example, I have an include for a banner image, the client would like a different image on each page so would I be able to do something along the lines of:

  <section id="subheader" class="{{subHeader}}"></section>

Then create an array:

var subHeader = ["pic1", "pic2", "pic3"];

The bit I am unsure of is how do I get each page to take a specific picture? Something like "if file is called page1 use 0 in the array"?

Thanks for your help!

My backup plan is to just create different includes if that seems easier than setting this up...


Solution

  • This is what I was needed:

    {% if pg2 %}
    <section id="subheader" class="subHeader banner2">
    </section>
    {% elseif pg3 %}
    <section id="subheader" class="subHeader banner3">
    </section>
    {% else pg4 %}
    <section id="subheader" class="subHeader banner4">
    </section>
    

    Then set each page to be the relevant name

    {% set pg2 = true %} 
    
    #skills