I follow the "Common template use cases" in play2.0 document, when I try to create a tag.
@(level: String = "error")(body: (String) => Html)
@level match {
case "success" => {
<p class="success">
@body("green")
</p>
}
case "warning" => {
<p class="warning">
@body("orange")
</p>
}
case "error" => {
<p class="error">
@body("red")
</p>
}
}
then refresh the page http://localhost:9000, get an error say:
')' expected but '=' found.
In foo/app/views/tags/notice.scala.html at line 4.
1#{extends 'main.html' /}
2#{set title:'notice.scala.html' /}
3
4@(level: String="error")(body: (String) => Html)
5
6@level match {
7
8 case "success" => {
since I am newbie in both play2.0 and scala, cloud someone tell me why?
It doesn't really make sense to have a defaulting argument in an argument group of its own:
@(level: String = "error")(body: (String) => Html)
Notice how the example "moreScripts and moreStyles" at Scala templates common use cases puts the defaulted argument with another argument:
@(title: String, scripts: Html = Html(""))(content: Html)
You can do the same:
@(body: (String) => Html, level: String = "error")
Side note: it isn't a very good idea to rely on Strings to distinguish between success/warning/error. Strings are fragile and subject to typos, which will hide bugs in annoying ways. Instead, look for a data type, or create your own, to represent this: that way typos become a compiler error.
class ResultType
case object Success extends ResultType
case class Warning(message: String) extends ResultType
case class Error(message: String) extends ResultType
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With