Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-05T08:55:52.377Z
Answers.Text: Elegant enough for me! I didn't know I could nest another ternary operation inside the first one. Thank you.
Answers.UserDisplayName: Florian
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-09T16:13:27.177Z
Answers.Text: 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.
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-05T08:47:47.693Z
Answers.LastActivityDate: 2014-09-05T08:47:47.693Z
Answers.UserDisplayName: André Elvan
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-07T12:21:54.067Z
Answers.LastActivityDate: 2015-07-07T12:21:54.067Z
Answers.UserDisplayName: certainlyakey
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-07T11:09:43.097Z
Answers.Text: Might be that `revindex0` didn't exist yet at the time I originally posted my question :)
Answers.UserDisplayName: Florian
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-04T10:35:05.220Z
Answers.LastActivityDate: 2019-01-04T10:35:05.220Z
Answers.UserDisplayName: Andy
Body: <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>
  
Tags: <templating><tags>
Title: How do I display tags comma separated with "and" between the last two tags?
ThreadText: # 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`](https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable):
  
  ```
  {% 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 `<li>` and `<ul>` 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 %}
  
  ```
  
  
  ---
  
  