<p>Not very elegant, but... :)</p>

<pre><code>{% for tag in tags %}
    {{ tag.type }}{{ loop.index &lt; loop.length-1 ? ', ' : loop.index &lt; loop.length ? ' and' }}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-09-05T08:55:52.377Z
Elegant enough for me! I didn't know I could nest another ternary operation inside the first one. Thank you.
Florian
CC BY-SA 3.0
2015-07-09T16:13:27.177Z
You could use `loop.revindex` or `loop.revindex0` to test how far you are from the end if you wanted to, for just a bit more elegance/obscurity.
Marion Newlevant
CC BY-SA 3.0
2014-09-05T08:47:47.693Z
2014-09-05T08:47:47.693Z
André Elvan
<p>You can output tags as a list (using <code>&lt;li&gt;</code> and <code>&lt;ul&gt;</code> tags) and add commas and <code>and</code> with CSS:</p>

<pre><code>li {display:inline}
li:before {content:', ';}
li:first-child:before {content:'';}
li:last-child:before {content:' and ';}
</code></pre>

<p>This will require that all the spaces between tags be removed so that your template code would look like this:</p>

<pre><code>{% for tag in tags %}{{ tag }}{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-07-07T12:21:54.067Z
2015-07-07T12:21:54.067Z
certainlyakey
<p>Bit of an old question, but here's a (subjectively) more elegant solution than the accepted answer using <a href="https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable" rel="nofollow noreferrer"><code>loop.revindex0</code></a>:</p>

<pre><code>{% for tag in tags %}
    {{ tag.type }}{{ loop.revindex0 == 1 ? ' and' : loop.revindex0 ? ', '  }}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-01-07T11:09:43.097Z
Might be that `revindex0` didn't exist yet at the time I originally posted my question :)
Florian
CC BY-SA 4.0
2019-01-04T10:35:05.220Z
2019-01-04T10:35:05.220Z
Andy
<p>I would like to output a tag-list like this: </p>

<p><strong>foo, bar and baz</strong></p>

<p>I know how to elegantly output a tag list with no comma after the last tag:</p>

<pre><code>{% for tag in tags %}
    {{ tag }}{{ not loop.last ? ', ' }}
{% endfor %}
</code></pre>

<p>Is there a similarly elegant solution for getting the tags to display the way I want?</p>

<templating><tags>
How do I display tags comma separated with "and" between the last two tags?
# How do I display tags comma separated with "and" between the last two tags?

(Asked by: username_0 on 2014-09-05)

I would like to output a tag-list like this: 
foo, bar and baz
I know how to elegantly output a tag list with no comma after the last tag:
{% for tag in tags %}
    {{ tag }}{{ not loop.last ? ', ' }}
{% endfor %}

Is there a similarly elegant solution for getting the tags to display the way I want?

**Tags:** templating, tags


---

## 3 answers

### Answer by username_4 on 2019-01-04. Score: 4 | Accepted answer

Bit of an old question, but here's a (subjectively) more elegant solution than the accepted answer using loop.revindex0:
{% for tag in tags %}
    {{ tag.type }}{{ loop.revindex0 == 1 ? ' and' : loop.revindex0 ? ', '  }}
{% endfor %}

> **Comments:**
> **username_0**: Might be that `revindex0` didn't exist yet at the time I originally posted my question :)  

---

### Answer by username_1 on 2014-09-05. Score: 6

Not very elegant, but... :)
{% for tag in tags %}
    {{ tag.type }}{{ loop.index < loop.length-1 ? ', ' : loop.index < loop.length ? ' and' }}
{% endfor %}

> **Comments:**
> **username_0**: Elegant enough for me! I didn't know I could nest another ternary operation inside the first one. Thank you.  
> **username_2**: You could use `loop.revindex` or `loop.revindex0` to test how far you are from the end if you wanted to, for just a bit more elegance/obscurity.  

---

### Answer by username_3 on 2015-07-07. Score: 1

You can output tags as a list (using  and  tags) and add commas and and with CSS:
li {display:inline}
li:before {content:', ';}
li:first-child:before {content:'';}
li:last-child:before {content:' and ';}

This will require that all the spaces between tags be removed so that your template code would look like this:
{% for tag in tags %}{{ tag }}{% endfor %}


---

