title: Loop through fields in Drupal 8
body: <p>I'm kind of new to Twig templating and Drupal 8 and I'm having a bit of trouble looping through some fields in a node template file. Basically, I have a Date field limited to a maximum of 2 fields, and if 2 fields are entered, I would like to display them like a date range and not just two random dates plonked on a page.</p>
  
  <p>I'm trying to achieve this by using a for loop/if statement to check if more than one field exists, and display the field output accordingly.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>  {% set dateLength = "" %}
    {% for date in content.field_date['#items'].getValue() %}
      {% set dateLength %}
        {{ loop.length }}
      {% endset %}
    {% endfor %}
  
    {% if dateLength == 2 %}
      {{ content.field_date['#items'].getValue()|last.value }}&amp;mdash;{{ content.field_date['#items'].getValue()|first.value }}
    {% else %}
      {{ content.field_date['#items'].getValue() }}
    {% endif %}
  </code></pre>
  
  <p>Let me know if I'm overthinking this. Any pointers would be much appreciated. Thanks for your help.</p>
  
  <p>Mark.</p>