Create an Array in Twig and loop through fields in global set So I've created a globals set (socialMedia) with 10 input fields, but this could grow in the future, for clients to add links to various social media accounts. This works fine except that the code for this seems overly long and reptitive since I need to put a conditional to check if each field has been output. Like this:

&lt;ul class="social list-inline"&gt;
  {% if socialMedia.facebook | length %}
    &lt;li&gt;&lt;a href="{{ socialMedia.facebook }}" target="_blank"&gt;&lt;i class="fa fa-facebook" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.twitter | length %}
    &lt;li&gt;&lt;a href="{{ socialMedia.facebook }}" target="_blank"&gt;&lt;i class="fa fa-twitter" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.flickr | length %}
  &lt;li&gt;&lt;a href="{{ socialMedia.flickr }}" target="_blank"&gt;&lt;i class="fa fa-flickr" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.instagram | length %}
  &lt;li&gt;&lt;a href="{{ socialMedia.instagram }}" target="_blank"&gt;&lt;i class="fa fa-instagram" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.email | length %}
  &lt;li&gt;&lt;a href="{{ socialMedia.email }}" target="_blank"&gt;&lt;i class="fa fa-envelope-o" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.vimeo | length %}
  &lt;li&gt;&lt;a href="{{ socialMedia.vimeo }}" target="_blank"&gt;&lt;i class="fa fa-vimeo-square" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.youtube | length %}
  &lt;li&gt;&lt;a href="{{ socialMedia.youtube }}" target="_blank"&gt;&lt;i class="fa fa-youtube" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.linkedin | length %}
  &lt;li&gt;&lt;a href="{{ socialMedia.linked }}" target="_blank"&gt;&lt;i class="fa fa-linkedin" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.tumblr | length %}
  &lt;li&gt;&lt;a href="{{ socialMedia.tumblr }}" target="_blank"&gt;&lt;i class="fa fa-tumblr-square" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.googlePlus | length %}
  &lt;li&gt;&lt;a href="{{ socialMedia.googlePlus }}" target="_blank"&gt;&lt;i class="fa fa-google-plus-official" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}

  {% if socialMedia.dribble | length %}
  &lt;li&gt;&lt;a href="{{ socialMedia.dribble }}" target="_blank"&gt;&lt;i class="fa fa-dribbble" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
  {% endif %}
&lt;/ul&gt;


Ideally I could set an array and then do a for loop and output everything as needed in a couple of lines. However I'm not sure how to proceed with that or if it is even possible.
Seems like a great use case for a Twig macro.  Completely untested, but something like this:

{% macro social(object, type) %}

    {% set value = attribute(object, type) %}

    {% if value | length %}
        &lt;li&gt;&lt;a href="{{ value }}" target="_blank"&gt;&lt;i class="fa fa-{{ type }}" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
    {% endif %}
{% endmacro %}

{% import "myMacros.html" as myMacros %}

{% set socialArray = ['facebook', 'twitter', 'instagram', 'vimeo', 'youtube'] %}

{% for socialType in socialArray %}
    {{ myMacros.social(socialMedia, socialType) }}
{% endfor %}