1

Ok, so I've tidied up the code a bit and using option.selected instead of option to see if the option is selected. But, it isn't behaving how I'd expect it:

{% for telephone in user.profilePhone.type('details') %}
    <li>
      {% if telephone.type == 'details' %}
        {% for option in telephone.icon.options %}
            {% if option.selected == 'mobile' %}
                <i class="fa fa-fw fa-mobile"></i>
            {% elseif option.selected == 'home' %}
                <i class="fa fa-fw fa-home"></i>
            {% else %}
                <i class="fa fa-fw fa-phone"></i>
            {% endif %}
        {% endfor %}
        {% if telephone.country %}{{ telephone.country }}{% endif %}
        {{ telephone.number }}
      {% endif %}
    </li>
{% endfor %}

So this is how I read it:

  1. Loop through all Block Types details
    • As I understood from the docs, there is no need to be specific if there's only one Block Type (regardless of max blocks setting). The extra if conditional seems redundant?
    • I also understood that a for loop would act as a conditional anyway, so it would return false if no Blocks existed?
  2. Loop through the icon dropdown options
    • Whichever one is selected, display it's corresponding icon.

But what it actually outputs is this:

Wrong icon output

So it's outputting all of the available options, not just the selected one (and not even the correct icons). I've managed (with separate if statements for just 'home' and 'mobile' to get this:

enter image description here

But it's still outputting all options declared in the if statement, instead of the desired single icon. Here are the dropdown options:

enter image description here

I'm new to programming, so perhaps I'm getting muddled up with things, but it seems like it should only output the icon I want, not the entire dropdown list (I realise I may need another conditional for if 'other' or 'work' is selected)

Perhaps the issue is you can't check in the for loop for just the selected option with an if condition. Something like:

{% for option in telephone.icon.options if option.selected %}

1

If I understand things correctly, you have a matrix block telephone, which contains a dropdown field telephone.icon, and you want the value of the selected dropdown option. That value is telephone.icon.value (which will be one of mobile, home, work, or other).

{% for telephone in user.profilePhone %}
  <li>
    {% if telephone.icon.value == 'mobile' %}
      <i class="fa fa-fw fa-mobile"></i>
    {% elseif telephone.icon.value == 'home' %}
      <i class="fa fa-fw fa-home"></i>
    {% else %}
      <i class="fa fa-fw fa-phone"></i>
    {% endif %}

    {% if telephone.country %}{{ telephone.country }}{% endif %}
    {{ telephone.number }}
  </li>
{% endfor %}
| improve this answer | |
  • Ah, seems to work great! I didn't realise you could access this outside of the {% for option in telephone.icon.options %} loop. So, presumably you could use a similar method for radio buttons etc — are there any snippets of this in the docs? It's not clear from the dropdown snippets yet it's pretty simple to do. Thanks! – Rob Jul 28 '15 at 17:04

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.