【问题标题】:Escape html in template PlayFramework在模板 PlayFramework 中转义 html
【发布时间】:2013-12-21 01:32:39
【问题描述】:

我想在我的模板中转义 HTML,但无论我尝试什么都行不通

这是我的模板:

<ul>
    @for(index <- 0 until appointments.size){
    <li>
        @(utils.DateUtil.getLocalDate(appointments(index).getStartDate())) - @appointments(index).getReason()
<ul>

@{val procedure = appointments(index).getProcedures()

    {if(procedure == null){
    <b>Empty</b>
} else {
    "&gt;b/&lt;NotEmpty&gt;/b&lt;" +
    procedure.size().toString+
    procedure.size().toString+
        <b>NotEmpty</b>+
    "<b>NotEmpty</b>"+
    "<b>NotEmpty</b>".toString;
}
}


            }


        </ul>

    </li>
    }

</ul>

有问题的代码在 else 分支中

我正在尝试将&lt;b&gt;NotEmpty&lt;/b&gt; 打印为 NotEmpty 但我只有纯文本,而不是 html

我试过@Html("&lt;strong&gt;Do not escape&lt;/strong&gt;"),但它说expected start of definition

如果我删除 else 分支的内容并留下

else {
    <b>NotEmpty</b>;
}

打印效果很好。 我正在使用使用 Scala 2.10.2 构建的 play framework 2.2.1(运行 Java 1.7.0_25)

【问题讨论】:

  • 只要用css把文字加粗就行了。
  • 这不是让文本变粗,而是打印出 html 标签,例如:

    some text

    some elements
  • 嗯只是好奇你为什么使用for这样的循环? @for(appointment &lt;- appointments) 不是更好吗?也可以使用 @if{}else{} 等。尝试先转换为标准 Play 语法并检查这是否能解决您的问题
  • 我不得不更改语法,因为我在块中声明了一个局部变量 -> @{val procedure =ointments(index).getProcedures()
  • 那么@defining(appointments(index).getProcedures()) { procedure =&gt; &lt;b&gt;@procedure&lt;/b&gt; }呢?

标签: scala templates playframework


【解决方案1】:

怎么样:

@{
  val x = Some(Seq("hi there")) 

  if(x.isDefined)
    <b>size = {x.get.size}</b><br/>
    <b>Not Empty</b>
  else
    <b>Empty</b>
}

【讨论】:

    猜你喜欢
    • 2014-08-10
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多