Mark Needham

Thoughts on Software Development

Scala/Mustache: Creating a comma separated list

with 8 comments

We’re using the Mustache templating engine on my project at the moment and one thing that we wanted to do was build a comma separated list.

Mustache is designed so that you pretty much can’t do any logic in the template which made it really difficult to do what we wanted.

It’s easy enough to get a comma after each item in a list with something like the following code:

{{#people}}<a href="/link/to/{{toString}}">{{toString}}</a>{{/people}}

where people is passed to the template as a collection of strings.

To get rid of the trailing comma we ended up building a collection of Pairs containing the person’s name and a boolean value indicating whether or not to show the comma.

We need to show the comma before every element except for the first one so we can pass the following collection to the template:

val values = names.zipWithIndex.map { case(item, index) => if(index == 0) (item, false) else (item, true) }

zipWithIndex returns a collection of pairs containing the original strings and their position in the collection.

We can then map to a different result for just the first element and then use those pairs in the template like so:

{{#people}} {{#_2}}, {{/_2}}<a href="/link/to/{{_1}}">{{_1}}</a>{{/people}}

It’s truly horrendous so if anyone knows a better way then please let me know!

Be Sociable, Share!

Written by Mark Needham

June 22nd, 2011 at 9:24 pm

Posted in Scala

Tagged with ,

  • Kenbot

    names mkString “,”

  • http://twitter.com/markhneedham Mark Needham

    mkString would work well if we just wanted to join strings together but we actually wanted to wrap each of the elements into a link tag as well and that type of logic really does go in the view!

  • David D

    slice off the last person, loop through the list appending the comma to the output, output the last person.

  • Kenbot

    Sorry, Mark, I misunderstood.   This is a more succinct version of the zipWithIndex line:

    val values = names zip (names map (names.head !=))

  • http://shuri.org/ shuri

    I was looking at mustache last week and was also wondering if there is a simpler way. It seems to me a disturbing limitation of mustache which otherwise seems nice.

  • Kenbot

    Actually changed my mind: this is better again, only one iteration necessary.

    val values = names map {n => (n, n != names.head)}

  • http://twitter.com/markhneedham Mark Needham

    @Kenbot – ah that’s a neat way of doing it! I always forget that we’re effectively doing a full iteration of the collection each time we use a map/zip so it does make more sense if you can do it in one go. 

  • Donny

    This use case is very neatly handled in the Liquid templating system , with a filter called ‘join’. http://liquid.rubyforge.org/classes/Liquid/StandardFilters.html#M000010