<p>You can use <a href="https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable" rel="nofollow noreferrer">loop variables</a></p>

<pre><code>{% set i = country|length %} // number of total items
{% set indexLast = i - 2 %}  // index of the last comma
{% for name in country %}
    {% if loop.last and i &gt; 1 %} and {% endif %}{{ name.title }}{%if i &gt; 2 and loop.index0 &lt; indexLast  %},{% endif %}
{% endfor %}
</code></pre>

<p>Explanation:</p>

<ul>
<li>if there is more than one item and it is the last iteration: add an <code>and</code> at the beginning</li>
<li>if there are more than 2 items (otherwise it's only one <code>and</code>) and the current loop index is smaller than <code>count - 2</code> ( it should display nothing in the last iteration and only an <code>and</code> before the last)-> display a comma <code>,</code> </li>
</ul>

CC BY-SA 4.0
2018-06-04T22:09:43.237Z
`Unexpected token "name" of value "last" ("end of statement block" expected).`.
Hellyar
CC BY-SA 4.0
2018-06-04T21:52:27.760Z
2018-06-04T22:27:27.150Z
2018-06-04T22:27:27.150Z
Robin Schambach
<p>I have a simple <code>for loop</code> to get the names of countries from a category.  </p>

<pre><code>{% for name in country %} {{ name.title }} {% endfor %}  
</code></pre>

<p>The output is typically returns the name of 1 country.  But, some entries are associated with more than one country.</p>

<p>For example: </p>

<blockquote>
  <p>Belgium Czech Republic Denmark Greece Netherlands Norway Portugal</p>
</blockquote>

<p><strong>How do I template for an entry that returns 2-N country names by adding <code>","</code> between each country and <code>"and"</code> before the last country?</strong>  </p>

<p>This is what I want to output:</p>

<blockquote>
  <p>Belgium, Czech Republic, Denmark, Greece, Netherlands, Norway and Portugal</p>
</blockquote>

CC BY-SA 4.0
2018-06-04T21:48:53.227Z
You can find the answer here: https://craftcms.stackexchange.com/questions/2377/how-do-i-display-tags-comma-separated-with-and-between-the-last-two-tags
Tad Ward
<templating>
How do I add commas to an output that returns a list of names?
# How do I add commas to an output that returns a list of names?

(Asked by: username_0 on 2018-06-04)

I have a simple for loop to get the names of countries from a category.  
{% for name in country %} {{ name.title }} {% endfor %}  

The output is typically returns the name of 1 country.  But, some entries are associated with more than one country.
For example: 

Belgium Czech Republic Denmark Greece Netherlands Norway Portugal

How do I template for an entry that returns 2-N country names by adding "," between each country and "and" before the last country? 
This is what I want to output:

Belgium, Czech Republic, Denmark, Greece, Netherlands, Norway and Portugal

**Tags:** templating
> **Comments:**
> **username_1**: You can find the answer here: https://craftcms.stackexchange.com/questions/2377/how-do-i-display-tags-comma-separated-with-and-between-the-last-two-tags  

---

## 1 answers

### Answer by username_2 on 2018-06-04. Score: 1

You can use loop variables
{% set i = country|length %} // number of total items
{% set indexLast = i - 2 %}  // index of the last comma
{% for name in country %}
    {% if loop.last and i > 1 %} and {% endif %}{{ name.title }}{%if i > 2 and loop.index0 < indexLast  %},{% endif %}
{% endfor %}

Explanation:

if there is more than one item and it is the last iteration: add an and at the beginning
if there are more than 2 items (otherwise it's only one and) and the current loop index is smaller than count - 2 ( it should display nothing in the last iteration and only an and before the last)-> display a comma ,

> **Comments:**
> **username_0**: `Unexpected token "name" of value "last" ("end of statement block" expected).`.  

---

