9

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

4 Answers 4

10

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 %}
2
  • Very clever and simple, ideal for tags :-)
    – noregt
    Commented Dec 18, 2014 at 16:41
  • Worked perfectly. Thank you! Commented Dec 13, 2021 at 19:52
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.

4
  • @noregt no love for the CSS solution?
    – carlcs
    Commented Aug 15, 2014 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
    Commented Aug 15, 2014 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
    Commented Aug 15, 2014 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
    Commented Aug 15, 2014 at 9:58
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) }}
5
  • Aha that's even better, so slice is the EE backspace alternative :-)
    – noregt
    Commented Aug 15, 2014 at 13:34
  • No, it's the worst way to do it in my opinion, @noregt!
    – carlcs
    Commented Aug 15, 2014 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
    Commented Aug 16, 2014 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
    Commented Aug 16, 2014 at 8:49
  • Oh, and it's your question. Don't feel being pushed to accept the CSS solution! (but do it!!!)
    – carlcs
    Commented Aug 16, 2014 at 8:51
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: "";
}
1
  • That is an option, but I would prefer if that can be done through craft. Or can't it?
    – noregt
    Commented Aug 15, 2014 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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