1

I'm trying to build a filter and for that I want my client to be able to select a few options, which I use as classes for in my filter.

I chose for a checkbox field and set it up in the backend. Now I'm trying to retrieve the data on the HTML side, but I can only retrieve ALL values, instead of only the selected ones.

This is how my HTML/twig markup looks like

{% for option in entry.offerMinimumPrice.options %} 
  {% if option.selected %}
    {{ option }}
  {% endif %}
{% endfor %}

With this code none of the values gets displayed in the front end. When I remove the conditional statement, all values get displayed (instead of only the selected ones).

Can anyone tell me what I'm doing wrong? Thanks!

1
  • The code you have should be working fine. Are you sure you're looking at the correct entry and that entry actually has something checked in the offerMinimumPrice field? – Brad Bell Sep 22 '15 at 2:55
2

The problem was that I used this code within an entry, so I had to use the tag of that entry (offer) instead of entry.

The code that works now is:

{% for option in offer.offerMinimumPrice.options %}
  {% if option.selected %}
    {{ option }}
  {% endif %}
{% endfor %}
2

If you want a comma-separated list of selected options, try this.

{% for option in offer.offerMinimumPrice.options|filter(v => v.selected) %}
    {{ option.label }}{% if loop.last==false %}, {% endif %}
{% endfor %}

for provides a loop object that provides many useful variables.

filter can be used to filter down items in a list or map using an arrow function. Given that selected is a boolean, you just need to return the value.

0

Try this instead:

{% for option in entry.offerMinimumPrice %} 
    {{ option }}
{% endfor %}
1
  • Hi, thanks for answering. Unfortunately, that doesn't return any values as well. – sanderfish Sep 21 '15 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.