Created
August 21, 2012 11:46
-
-
Save mariussoutier/3414818 to your computer and use it in GitHub Desktop.
More template sequence helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package views.html.helper | |
import play.api.templates.Html | |
// Executes the first block if non-empty, the second otherwise | |
// Passes the entire seq (as in defining) | |
object ifEmptyOrElse { | |
def apply[T <: Seq[_]](t: T)(nonEmptyBlock: (T) => Html)(emptyBlock: => Html) = { | |
if (t.nonEmpty) nonEmptyBlock(t) else emptyBlock | |
} | |
} | |
// Executes the first block if non-empty, the second otherwise | |
// Maps each element | |
object mapOrElse { | |
def apply[T](t: Seq[T])(nonEmptyBlock: (T) => Html)(emptyBlock: => Html) = { | |
if (t.nonEmpty) t.map(nonEmptyBlock(_)) else emptyBlock | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment