使用Play Framework 2.3.0检查Html片段无效

时间:2014-09-25 14:20:39

标签: templates playframework-2.3 twirl

我的应用程序中有一个标记,结构如下:

@(
    columns: Integer
)(header: Html)(body: Html)

<table>
    @if(header != null) {
        <thead>
            <tr>
                @header
            </tr>
        </thead>
    }
    // Body and foot here
</table>

我在我的模板中使用它:

@tags.Table(5) { } {
    // My content here
}

前面的代码不起作用:即使我让括号为空,也会显示<thead></thead>。那么如何检查header是否为空,为空...以及如何在模板中声明我的标签? 也许我错误地用{ }声明了它?

如果我用{}声明,我有以下错误:

type mismatch;
 found   : Unit
 required: play.twirl.api.Html

1 个答案:

答案 0 :(得分:4)

twirl模板编译器将空大括号推断为返回Unit的call-by-value参数。您不能只是将大括号留空并期望它通过null

只需将Html空对象作为header传递,并在打印之前检查body的{​​{1}}是否为空。

header

并称之为:

@(columns: Int)(header: Html)(body: Html)
<table>
    @if(header.body.nonEmpty) {
        <thead>
            <tr>@header</tr>
        </thead>
    }
    @* ... etc .. *@  
</table>
相关问题