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


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 %}
| improve this answer | |
  • Very clever and simple, ideal for tags :-) – noregt Dec 18 '14 at 16:41

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.

| improve this answer | |
  • @noregt no love for the CSS solution? – carlcs Aug 15 '14 at 9:46
  • Not really, if it's a list I'd also rather not have the separator in the html. (comma, backslash or similar) – noregt Aug 15 '14 at 9:49
  • 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 Aug 15 '14 at 9:51
  • 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 Aug 15 '14 at 9:58

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) }}
| improve this answer | |
  • Aha that's even better, so slice is the EE backspace alternative :-) – noregt Aug 15 '14 at 13:34
  • No, it's the worst way to do it in my opinion, @noregt! – carlcs Aug 15 '14 at 13:35
  • 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 Aug 16 '14 at 8:33
  • 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 Aug 16 '14 at 8:49
  • Oh, and it's your question. Don't feel being pushed to accept the CSS solution! (but do it!!!) – carlcs Aug 16 '14 at 8:51

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: "";
| improve this answer | |
  • That is an option, but I would prefer if that can be done through craft. Or can't it? – noregt Aug 15 '14 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.