Answers.Body: <p>Seems like a great use case for a <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="nofollow noreferrer">Twig macro</a>.  Completely untested, but something like this:</p>
  
  <pre><code>{% 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 %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-13T18:00:42.770Z
Answers.Text: Brad, that looks like what I'm trying to do. I'm probably being dense here, but I'm not sure how I grab from each of the fields in the socialMedia global. I'm still very much a noob with Craft and twig.
Answers.UserDisplayName: CreateSean
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-13T18:18:42.463Z
Answers.Text: Updated answer.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-13T19:09:26.320Z
Answers.Text: Brad, this works perfectly thank you. Did notice that some of the font-awesome classes have dashes in the name for example vimeo-square which won't work since handles in Craft can't have dashes. I've switched to classes without dashes, but would like to know if there is a work around for that.
Answers.UserDisplayName: CreateSean
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-13T17:54:20.937Z
Answers.LastActivityDate: 2016-11-13T18:17:47.130Z
Answers.LastEditDate: 2016-11-13T18:17:47.130Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>Why not just use a single table field or SuperTable (plugin) field and iterate over its rows?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-13T17:48:46.390Z
Answers.Text: Trying to avoid 3rd party plugins so no supertable. And regular table doesn't have a dropdown option to have clients select social media for that row.
Answers.UserDisplayName: CreateSean
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-13T17:47:06.880Z
Answers.LastActivityDate: 2016-11-13T17:47:06.880Z
Answers.UserDisplayName: Patrick Harrington
Body: <p>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:</p>
  
  <pre><code>&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;
  </code></pre>
  
  <p>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.</p>
  
Tags: <templating><globals><array>
Title: Create an Array in Twig and loop through fields in global set
ThreadText: # Create an Array in Twig and loop through fields in global set
  
  (Asked by: username_0 on 2016-11-13)
  
  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:
  
  ```
  <ul class="social list-inline">
    {% if socialMedia.facebook | length %}
      <li><a href="{{ socialMedia.facebook }}" target="_blank"><i class="fa fa-facebook" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.twitter | length %}
      <li><a href="{{ socialMedia.facebook }}" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.flickr | length %}
    <li><a href="{{ socialMedia.flickr }}" target="_blank"><i class="fa fa-flickr" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.instagram | length %}
    <li><a href="{{ socialMedia.instagram }}" target="_blank"><i class="fa fa-instagram" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.email | length %}
    <li><a href="{{ socialMedia.email }}" target="_blank"><i class="fa fa-envelope-o" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.vimeo | length %}
    <li><a href="{{ socialMedia.vimeo }}" target="_blank"><i class="fa fa-vimeo-square" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.youtube | length %}
    <li><a href="{{ socialMedia.youtube }}" target="_blank"><i class="fa fa-youtube" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.linkedin | length %}
    <li><a href="{{ socialMedia.linked }}" target="_blank"><i class="fa fa-linkedin" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.tumblr | length %}
    <li><a href="{{ socialMedia.tumblr }}" target="_blank"><i class="fa fa-tumblr-square" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.googlePlus | length %}
    <li><a href="{{ socialMedia.googlePlus }}" target="_blank"><i class="fa fa-google-plus-official" aria-hidden="true"></i></a></li>
    {% endif %}
  
    {% if socialMedia.dribble | length %}
    <li><a href="{{ socialMedia.dribble }}" target="_blank"><i class="fa fa-dribbble" aria-hidden="true"></i></a></li>
    {% endif %}
  </ul>
  
  ```
  
  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.
  
  **Tags:** templating, globals, array
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-11-13. Score: 2 | Accepted answer
  
  Seems like a great use case for a [Twig macro](http://twig.sensiolabs.org/doc/tags/macro.html). Completely untested, but something like this:
  
  ```
  {% macro social(object, type) %}
  
      {% set value = attribute(object, type) %}
  
      {% if value | length %}
          <li><a href="{{ value }}" target="_blank"><i class="fa fa-{{ type }}" aria-hidden="true"></i></a></li>
      {% endif %}
  {% endmacro %}
  
  {% import "myMacros.html" as myMacros %}
  
  {% set socialArray = ['facebook', 'twitter', 'instagram', 'vimeo', 'youtube'] %}
  
  {% for socialType in socialArray %}
      {{ myMacros.social(socialMedia, socialType) }}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: username_1, that looks like what I'm trying to do. I'm probably being dense here, but I'm not sure how I grab from each of the fields in the socialMedia global. I'm still very much a noob with Craft and twig.  
  > **username_1**: Updated answer.  
  > **username_0**: username_1, this works perfectly thank you. Did notice that some of the font-awesome classes have dashes in the name for example vimeo-square which won't work since handles in Craft can't have dashes. I've switched to classes without dashes, but would like to know if there is a work around for that.  
  
  ---
  
  ### Answer by username_2 on 2016-11-13. Score: 1
  
  Why not just use a single table field or SuperTable (plugin) field and iterate over its rows?
  
  > **Comments:**
  > **username_0**: Trying to avoid 3rd party plugins so no supertable. And regular table doesn't have a dropdown option to have clients select social media for that row.  
  
  ---
  
  