Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with Options in Scala Play Template

I am trying reference things of type option in my Scala Play template. I've been trying to use this resource: http://www.playframework.com/modules/scala-0.9/templates

This is how I am trying to reference a field in the case class:

@{optionalobject ?. field}

and it is not working, this is the error I am getting:

';' expected but '.' found.

I am unsure why I am getting this error.

like image 736
Lilluda 5 Avatar asked Dec 12 '13 20:12

Lilluda 5


2 Answers

For slightly nicer formatting that can span many lines (if you need to):

@optionalObject match {
  case Some(o) => {
    @o.field
  }
  case None => {
    No field text/html/whatever
  }
}

Or if you don't want to display anything if the field isn't defined:

@for(o <- optionalObject) {
  @o.field
}
like image 176
James Roper Avatar answered Oct 13 '22 17:10

James Roper


@optionalobject.map(o => o.field).getOrElse("default string if optionalobject is None")
like image 37
josephpconley Avatar answered Oct 13 '22 15:10

josephpconley