简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM