1

I've set up a dropdown field for the client to choose from a list of pre-defined addresses. In my template, I want to determine whether the field has a value or not. When I look in the database, Entry A has a value of 'NULL', while Entry B has a value of 'address1'. So the field is working properly in those regards.

However, in my template it's all or nothing. Here is my code:

{% if event.includeMap|length %}
<tr>
    <th class="small">
        Defined? {{ event.includeMap is defined }}<br>
        Not Null? {{ event.includeMap is not null }}<br>
        Null: {{ event.includeMap is null }}<br>
        Value: {{ event.includeMap }}
    </th>
    <td align="center">
        {% include 'partials/_inc-map' %}
    </td>
</tr>
{% endif %}

I've added the options in the left column to see what the database returns. In both cases, they return the same values. However, with Entry A, nothing is returned as a value, and in Entry B, the correct value appears.

I have also tried

{% if event.includeMap is defined %}
{% if event.includeMap is not null %}

Any other thoughts?

  • How does your dropdown not have anything selected? Do you have an empty label/value at the top or something and you consider that nothing selected? – Brad Bell Mar 10 '17 at 1:09
  • I have the first option as a "Please Select...." with nothing as a value (which someone recommended in another post). Am I better off adding a value of 0 or something and testing against that? – Todd Mar 10 '17 at 5:04
4

If you've got a dropdown field defined with no value as one of the options, it should be as simple as:

{% if entry.dropdownFieldHandle %}
    {{ entry.dropdownFieldHandle }}
{% endif %}
  • Thanks Brad. I'm going to mark this as the correct answer because it does work. However, it is showing content inside the {% if %} statement even if a value has been selected from the dropdown, which is strange. No worries though, I am modifying my template to avoid that. – Todd Mar 10 '17 at 17:44

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.