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
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.
-
-
Not really, if it's a list I'd also rather not have the separator in the html. (comma, backslash or similar)– noregtCommented 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.– carlcsCommented 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.– noregtCommented Aug 15, 2014 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) }}
-
Aha that's even better, so slice is the EE backspace alternative :-)– noregtCommented Aug 15, 2014 at 13:34
-
-
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.– noregtCommented 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.– carlcsCommented 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!!!)– carlcsCommented Aug 16, 2014 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: "";
}
-
That is an option, but I would prefer if that can be done through craft. Or can't it?– noregtCommented Aug 15, 2014 at 9:20