Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement pluralize & other extensions using Playframework 2.0

In playframework 1.x there used to be some bundled java extensions for the templating engine: http://www.playframework.org/documentation/1.2.3/javaextensions

I'm looking for the same functionality in playframework 2.0. For example how would I do this?

colour${['red', 'green', 'blue'].pluralize()} 

I am doing this malually now:

We have @colours.size colour@if(colours.size > 0){s}

the must be a cleaner more reusable way to do this?

like image 768
Somatik Avatar asked Mar 19 '12 15:03

Somatik


1 Answers

You can leverage the pimp my lib Scala pattern to implement something equivalent to Play 1.x Java extensions.

For example, the pluralize method on collection can be implemented as follows:

// File app/views/pimps.scala
package views

package object pimps {
  class PimpedTraversable[A](col: Traversable[A]) {
    def pluralize = if (col.size == 1) "" else "s"
  }

  implicit def pimpTraversable[A](col: Traversable[A]) = new PimpedTraversable(col)
}

You can then use it as follows:

@import views.pimps._

We have @colours.size [email protected]
like image 168
Julien Richard-Foy Avatar answered Sep 28 '22 07:09

Julien Richard-Foy