Remove class to element by toggle field

Hi,

Sorry in advance if this question sounds dumb. I have a very basic page where I display product cards. In the blueprints I have the field

togglepromo:
        label: Promo
        type: toggle
        text:
          - "no"
          - "yes" 

And in my template I have a list of items

<li class="product-card promo"></li>

I would need to remove the class .promo when the toggle on the blueprints is on “no”, but I don’t know how to do it. Would somebody help me with this? I’ve seen similar topics but I wasn’t able to apply it to this case.

Thank you very much!

Hello,

You can use something like this in your template:

<li class="product-card <?php if ($page->togglepromo()->toBool()===true):?>
promo<?php endif ?>"></li>

Maybe there’s a better way to do it but it works for me :slight_smile:

There’s an echo statement missing in your code. And instead of the if-statement, I’d prefer using the ternary operator:

<li class="product-card<?= $page->togglepromo()->toBool() ?  ' promo' : '' ?>"></li>
2 Likes

Perfect. Now it works! Thank you so much valentin and texnixe :pray:

1 Like