How to implement pluralize & other extensions using Playframework 2.0

给你一囗甜甜゛ 提交于 2019-12-03 14:27:14

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 colour@colours.pluralize
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!