如何实现多元化使用Playframework 2.0的其他扩展 [英] How to implement pluralize & other extensions using Playframework 2.0

查看:84
本文介绍了如何实现多元化使用Playframework 2.0的其他扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在playframework 1.x中,模板引擎曾经捆绑了一些Java扩展: http://www.playframework.org/documentation/1.2.3/javaextensions

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

我正在寻找Playframework 2.0中的相同功能.例如,我该怎么做?

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?

推荐答案

您可以利用等同于Play 1.x Java的东西扩展名.

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

例如,关于集合的pluralize方法可以实现如下:

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

这篇关于如何实现多元化使用Playframework 2.0的其他扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆