<p>There's a couple of <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable">special variables</a> available while for looping through an array. In your case I'd make use of <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable"><code>loop.last</code></a>.</p>

<pre><code>{{ tag }}{{ not loop.last ? ', ' }}
</code></pre>

<p>This is the expression using the <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators">ternary operator</a> to keep the syntax short.</p>

CC BY-SA 3.0
2014-08-15T09:46:54.683Z
@noregt no love for the CSS solution?
carlcs
CC BY-SA 3.0
2014-08-15T09:49:25.817Z
Not really, if it's a list I'd also rather not have the separator in the html. (comma, backslash or similar)
noregt
CC BY-SA 3.0
2014-08-15T09:51:14.073Z
That's the exact reason to use CSS over HTML, @noregt! You'd only apply the class to those inline lists you wanna STYLE like this.
carlcs
CC BY-SA 3.0
2014-08-15T09:58:45.100Z
I meant the LAST parsed separator, but I get your point if you're working with official lists. I was working with a simple paragraph line and comma's. But I admit that's not very good html.
noregt
CC BY-SA 3.0
2014-08-15T09:29:19.693Z
2014-08-15T09:29:19.693Z
carlcs
<p>If you you have a given string and can't influence how it is generated, use the <a href="http://twig.sensiolabs.org/doc/filters/slice.html" rel="noreferrer"><code>slice</code></a> filter to crop that last comma:</p>

<pre><code>{% set tags = 'Alpirsbacher Klosterbräu, Rothaus Tannenzäpfle, Neumarkter Lammsbräu, ' %}

{# Remove the last two chars ", " from the string #}
{{ tags|slice(0, -2) }}
</code></pre>

CC BY-SA 3.0
2014-08-15T13:34:04.290Z
Aha that's even better, so slice is the EE backspace alternative :-)
noregt
CC BY-SA 3.0
2014-08-15T13:35:25.520Z
No, it's the worst way to do it in my opinion, @noregt!
carlcs
CC BY-SA 3.0
2014-08-16T08:33:42.503Z
Haha I understand your annoyance, but essentially I wanted to know the alternative for the EE backspace parameter. You changed the title, but that was what I meant to ask.
noregt
CC BY-SA 3.0
2014-08-16T08:49:23.687Z
Yep, I think the new title fits this Q/A better. You asked for a way to make your tag list and that EE thing was just a side note for me (yes, there are people using Craft NOT coming from EE :D). Cheers, @noregt.
carlcs
CC BY-SA 3.0
2014-08-16T08:51:24.433Z
Oh, and it's your question. Don't feel being pushed to accept the CSS solution! (but do it!!!)
carlcs
CC BY-SA 3.0
2014-08-15T13:32:15.817Z
2014-08-15T13:32:15.817Z
carlcs
<p>What about using CSS for that?</p>

<pre class="lang-css prettyprint-override"><code>.u-inlineList {
  display: inline;
}

.u-inlineList-item {
  display: inline;
  list-style: none;
}

.u-inlineList-item:after {
  content: ", ";
}

.u-inlineList-item:last-child:after {
  content: "";
}
</code></pre>

CC BY-SA 3.0
2014-08-15T09:20:00.970Z
That is an option, but I would prefer if that can be done through craft. Or can't it?
noregt
CC BY-SA 3.0
2014-08-15T09:17:06.823Z
2014-08-15T09:17:06.823Z
carlcs
<p>An alternative, if you are getting the tags from the <a href="http://buildwithcraft.com/docs/tags-fields">Tag Field</a> type, is the <a href="http://twig.sensiolabs.org/doc/filters/join.html">Join</a> filter:</p>

<pre><code>{% if post.tags | length %}
    Tags: {{ post.tags | join(', ') }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-12-18T16:41:27.700Z
Very clever and simple, ideal for tags :-)
noregt
CC BY-SA 4.0
2021-12-13T19:52:18.643Z
Worked perfectly. Thank you!
Anna_MediaGirl
CC BY-SA 3.0
2014-11-19T19:42:24.950Z
2014-11-19T19:42:24.950Z
mattl
<p>In a list of tags I'd like to remove the last comma. So is there something that works like the Ee backspace parameter?</p>

<templating><tags><conditional><forloop>
Remove the last comma in an inline list of tags?
# Remove the last comma in an inline list of tags?

(Asked by: username_0 on 2014-08-15)

In a list of tags I'd like to remove the last comma. So is there something that works like the Ee backspace parameter?

**Tags:** templating, tags, conditional, forloop


---

## 4 answers

### Answer by username_2 on 2014-11-19. Score: 10 | Accepted answer

An alternative, if you are getting the tags from the Tag Field type, is the Join filter:
{% if post.tags | length %}
    Tags: {{ post.tags | join(', ') }}
{% endif %}

> **Comments:**
> **username_0**: Very clever and simple, ideal for tags :-)  
> **username_3**: Worked perfectly. Thank you!  

---

### Answer by username_1 on 2014-08-15. Score: 18

There's a couple of special variables available while for looping through an array. In your case I'd make use of loop.last.
{{ tag }}{{ not loop.last ? ', ' }}

This is the expression using the ternary operator to keep the syntax short.

> **Comments:**
> **username_1**: @username_0 no love for the CSS solution?  
> **username_0**: Not really, if it's a list I'd also rather not have the separator in the html. (comma, backslash or similar)  
> **username_1**: That's the exact reason to use CSS over HTML, @username_0! You'd only apply the class to those inline lists you wanna STYLE like this.  
> **username_0**: I meant the LAST parsed separator, but I get your point if you're working with official lists. I was working with a simple paragraph line and comma's. But I admit that's not very good html.  

---

### Answer by username_1 on 2014-08-15. Score: 5

If you you have a given string and can't influence how it is generated, use the slice filter to crop that last comma:
{% set tags = 'Alpirsbacher Klosterbräu, Rothaus Tannenzäpfle, Neumarkter Lammsbräu, ' %}

{# Remove the last two chars ", " from the string #}
{{ tags|slice(0, -2) }}

> **Comments:**
> **username_0**: Aha that's even better, so slice is the EE backspace alternative :-)  
> **username_1**: No, it's the worst way to do it in my opinion, @username_0!  
> **username_0**: Haha I understand your annoyance, but essentially I wanted to know the alternative for the EE backspace parameter. You changed the title, but that was what I meant to ask.  
> **username_1**: Yep, I think the new title fits this Q/A better. You asked for a way to make your tag list and that EE thing was just a side note for me (yes, there are people using Craft NOT coming from EE :D). Cheers, @username_0.  
> **username_1**: Oh, and it's your question. Don't feel being pushed to accept the CSS solution! (but do it!!!)  

---

### Answer by username_1 on 2014-08-15. Score: 3

What about using CSS for that?
.u-inlineList {
  display: inline;
}

.u-inlineList-item {
  display: inline;
  list-style: none;
}

.u-inlineList-item:after {
  content: ", ";
}

.u-inlineList-item:last-child:after {
  content: "";
}

> **Comments:**
> **username_0**: That is an option, but I would prefer if that can be done through craft. Or can't it?  

---

