Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 为什么在play 2 scala模板中构建的html会产生一个空的html案例类_Templates_Scala_Pattern Matching_Playframework 2.0 - Fatal编程技术网

Templates 为什么在play 2 scala模板中构建的html会产生一个空的html案例类

Templates 为什么在play 2 scala模板中构建的html会产生一个空的html案例类,templates,scala,pattern-matching,playframework-2.0,Templates,Scala,Pattern Matching,Playframework 2.0,为什么输出“html为空” @h={ @h匹配{ 大小写Html(“”=>{Html为空} 案例{html有内容} } 这会输出“html有内容” @i=@{Html()} @我匹配{ 大小写Html(“”=>{Html为空} 案例{html有内容} } 这对我很重要的原因是,在上一个示例中(moreScripts和moreStyles等价物),第一个样式用于将一堆html传递到另一个模板中。然后我想根据html是否包含内容进行切换。但它总是与Html(“”)匹配。也许可以尝试进行字符串匹配

为什么输出“html为空”

@h={
@h匹配{ 大小写Html(“”=>{Html为空} 案例{html有内容} }
这会输出“html有内容”

@i=@{Html(
)} @我匹配{ 大小写Html(“”=>{Html为空} 案例{html有内容} }

这对我很重要的原因是,在上一个示例中(moreScripts和moreStyles等价物),第一个样式用于将一堆html传递到另一个模板中。然后我想根据html是否包含内容进行切换。但它总是与Html(“”)匹配。

也许可以尝试进行字符串匹配

@h.toString() match {
  case "" => {html is empty}
  case _ => {html has content}
}

为每个方法生成的代码略有不同

def h:play.api.templates.Html = {
  _display_(
    Seq[Any](format.raw("""<br />"""))
  )
}
def i = {{Html("<br />")}}
Html
case类由一个可变的
StringBuilder
支持

case class Html(text: String) extends Appendable[Html] with Content with play.mvc.Content {
  val buffer = new StringBuilder(text)

  /**
   * Appends this HTML fragment to another.
   */
  def +(other: Html): Html = {
    buffer.append(other.buffer)
    this
  }
  override def toString = buffer.toString

  /**
   * Content type of HTML (`text/html`).
   */
  def contentType: String = "text/html"

  def body: String = toString

}
这意味着
text
的值只会被设置为第一个
Html
text
值。每当通过
+
方法创建新的
Html
时,您只需修改
StringBuilder

case class Html(text: String) extends Appendable[Html] with Content with play.mvc.Content {
  val buffer = new StringBuilder(text)

  /**
   * Appends this HTML fragment to another.
   */
  def +(other: Html): Html = {
    buffer.append(other.buffer)
    this
  }
  override def toString = buffer.toString

  /**
   * Content type of HTML (`text/html`).
   */
  def contentType: String = "text/html"

  def body: String = toString

}
例如

由于模式匹配使用的是
文本
值,因此有效地破坏了其用于模式匹配的能力

例如


当您输出
@h
-空字符串时会发生什么情况?@h和@i都输出了一个编译失败的
,并且在某种程度上违背了模式匹配的目的。我想知道为什么两种形式的作业只能导致一种模式匹配。
Html("") + Html("<br />")
case class Html(text: String) extends Appendable[Html] with Content with play.mvc.Content {
  val buffer = new StringBuilder(text)

  /**
   * Appends this HTML fragment to another.
   */
  def +(other: Html): Html = {
    buffer.append(other.buffer)
    this
  }
  override def toString = buffer.toString

  /**
   * Content type of HTML (`text/html`).
   */
  def contentType: String = "text/html"

  def body: String = toString

}
val html = Html("1") + Html("2")
html.text == "1"
html.toString == "12"
html.body == "12"
(Html("1") + Html("2")) match { case Html("1") => "broken" } // returns "broken"