Sometimes itโs convenient to write a short helper when working with Option to declare the template code:
// Helper object is defined in some file Helper.scala object Helper { def maybeAttribute[T](attrName:String, maybeValue:Option[String]) = maybeValue.fold("") { value => Html(attrName + "=" + value).toString() } }
The template can then use this helper method directly, as
// some view.scala.html file <div @Helper.maybeAttribute("id",maybeId)> </div>
source share