Skip to content

Instantly share code, notes, and snippets.

@mariussoutier
Created August 21, 2012 11:46
Show Gist options
  • Save mariussoutier/3414818 to your computer and use it in GitHub Desktop.
Save mariussoutier/3414818 to your computer and use it in GitHub Desktop.
More template sequence helpers
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