Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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) }}
share|improve this answer
    
Aha that's even better, so slice is the EE backspace alternative :-) –  noregt Aug 15 at 13:34
    
No, it's the worst way to do it in my opinion, @noregt! –  carlcs Aug 15 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 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 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 at 8:51

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.

share|improve this answer
    
That did it, thanks! –  noregt Aug 15 at 9:46
    
@noregt no love for the CSS solution? –  carlcs Aug 15 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 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 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 at 9:58

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: "";
}
share|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 at 9:20
    
Sure you can, but I'd prefer the CSS solution, as you further separate content form styling. –  carlcs Aug 15 at 9:31
    
OK, it's probably a matter of opinion, if you consider those commas being content or not, @noregt! –  carlcs Aug 15 at 9:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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