TWIG for loop returning first value only I have an array of 5 objects. I would like to loop throught the 5 objects and display fields from within.



I have a for loop in place that is returning the appropriate fields, but it is displaying the same object 5 times.

{% for key, object in featured_resource_nodes %}

  {% if node.field_featured_resources[0].entity.field_resource_type.value == 'whitepaper' %}
    &lt;span class="icon-icons_whitepaper"&gt;&lt;/span&gt;
  {% elseif node.field_featured_resources[0].entity.field_resource_type.value == 'Datasheet' %}
    &lt;span class="icon-icons_datasheet"&gt;&lt;/span&gt;
  {% elseif node.field_featured_resources[0].entity.field_resource_type.value == 'blog' %}
    &lt;span class="icon-icons_blog"&gt;&lt;/span&gt;
  {% elseif node.field_featured_resources[0].entity.field_resource_type.value == 'video' %}
    &lt;span class="icon-icons_video"&gt;&lt;/span&gt;
  {% elseif node.field_featured_resources[0].entity.field_resource_type.value == 'analystreport' %}
    &lt;span class="icon-icons_analyst-report"&gt;&lt;/span&gt;     
  {% endif %} 

  {% if node.field_featured_resources[0].entity.field_resource_type.value == 'video' %}    
    &lt;a href="{{ featured_resource_nodes[0].field_on_demand_webinar[0].data[0] }}"&gt;&lt;h3 class="text-center"&gt;{{ node.field_featured_resources.entity.title.value }}&lt;/h3&gt;&lt;/a&gt; 
  {% else %}
    &lt;a href="{{ file_url(node.field_featured_resources.entity.field_resource_file[0].entity.uri.value) }}"&gt;&lt;h3 class="text-center"&gt;{{ node.field_featured_resources.entity.title.value }}&lt;/h3&gt;&lt;/a&gt; 
  {% endif %}

{% endfor %}


But this is what is being returned



How do I loop through the objects and have it display the 5 individual results?
It is displaying the same object 5 times because node.field_featured_resources[0] is referenced 5 times. Use the iterator variable object or use node.field_featured_resources[key] instead.

It also may be better to do this without all of the logic in the template. Off of the top of my head,


In template preprocess for the field (PHP), set the class in Attribute.
In module layer (PHP), add a Field Formatter.


Or if Field Formatter are already available, configure Manage Display to display the fields in the desired format.

In template (Twig), print out the field value in the span directly.


Here's some Twig:

{% for key, object in featured_resource_nodes %}
  &lt;span class="icon-icons_{{ object.entity.field_resource_type.value }}"&gt;&lt;/span&gt;

  {% if object.entity.field_resource_type.value == 'video' %}
    {# @todo: Configure Field Display for On-Demand Webinar instead... #}
    &lt;a href="{{ object.field_on_demand_webinar[0].data[0] }}"&gt;
      {# @todo: consider object.entity.title instead of .value #}
      &lt;h3 class="text-center"&gt;{{ object.entity.title.value }}&lt;/h3&gt;
    &lt;/a&gt;
  {% else %}
    {# @todo: Configure Field Display for Resource File instead... #}
    &lt;a href="{{ file_url(object.entity.field_resource_file[0].entity.uri.value) }}"&gt;
      {# @todo: consider object.entity.title instead of .value #}
      &lt;h3 class="text-center"&gt;{{ object.entity.title.value }}&lt;/h3&gt;
    &lt;/a&gt; 
  {% endif %}
{% endfor %}