简体   繁体   English

播放框架scala模板与枚举的case语句

[英]Play framework scala template with case statement on enumeration

I have this scala template and want to use a case statement to render different html based on a matching enum value. 我有这个scala模板,并希望使用case语句根据匹配的枚举值呈现不同的html。

My template looks like this: 我的模板看起来像这样:

@(tagStatus: String)

 try {
   TagStatus.withName(tagStatus) match {
         case TagStatus.deployed => {<span class="label label-success">@tagStatus</span>}
         case TagStatus.deployedPartially => {<span class="label label-important">@tagStatus</span>}
         case TagStatus.deployedWithProblems => {<span class="label label-important">@tagStatus</span>}
     }
 } catch {
    {<span class="label label-important">??</span>}
 }

The enum looks something like this: 枚举看起来像这样:

object TagStatus extends Enumeration{
   val deployed = Value("deployed")
   val deployedWithProblems = Value("deployed_with_problems")
   val deployedPartially = Value("deployed_partially")     
}

When i run this i get: 当我运行这个我得到:

Compilation error
')' expected but 'case' found.
In C:\...\statusTag.scala.html at line 8.
5        case TagStatus.deployed => {<span class="label label-success">@tagStatus</span>}
6        case TagStatus.deployedPartially => {<span class="label label-important">@tagStatus</span>}
7        case TagStatus.deployedWithProblems => {<span class="label label-important">@tagStatus</span>}
8    } 
9 } catch {
10    {<span class="label label-important">??</span>}
11 }

I have not idea what is meant by this error message. 我不知道此错误消息的含义。

What am i missing in order to get this simple code snippet to compile? 为了让这个简单的代码片段能够编译,我缺少什么?

Thanks! 谢谢!

toString is not compatible with match so convert the String to the enum using withName toString与match不兼容,因此使用withName将String转换为枚举

You can do it like this - I am not quite sure of the Play syntax: 您可以这样操作-我不太确定Play语法:

TagsStatus.withName(tagStatus) match {
  case TagStatus.deployed => {<span class="label label-success">@tagStatus</span>}
  case TagStatus.deployedPartially => {<span class="label label-important">@tagStatus</span>}
  case TagStatus.deployedWithProblems => {<span class="label label-important">@tagStatus</span>}
  case _ => {<span class="label label-important">??</span>}
}

BTW there is a common related issue concerning Scala pattern matching with lower case variable names BTW有一个与小写变量名相关的Scala模式匹配的常见相关问题

You don't have to use try here, just the wild card in you match (see: http://www.playframework.com/documentation/2.1.x/ScalaTemplateUseCases ). 您不必在此处使用try,只需匹配其中的通配符即可(请参阅: http : //www.playframework.com/documentation/2.1.x/ScalaTemplateUseCases )。

@(tagStatus: String)

@tagStatus match {
    case TagStatus.deployed.toString => {<span class="label label-success">@tagStatus</span>}
    case TagStatus.deployedPartially.toString => {<span class="label label-important">@tagStatus</span>}
    case TagStatus.deployedWithProblems.toString => {<span class="label label-important">@tagStatus</span>}
    case _ => {<span class="label label-important">??</span>}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM