变量不在play2模板上解释

时间:2014-06-05 14:47:59

标签: java playframework playframework-2.2

Play2模板引擎存在问题。在渲染过程中不会解释某些变量。

变量{key.getKey}在被双引号推翻时不会被解释。我怎么能解决这个问题?

播放2.2.3模板(index.scala.html)

    @flash
    @(if (flash.size > 0) {
        flash.entrySet.iterator.map { key =>
            <div class="row">
                <div class="large-12 columns">
                    <div class="alert-box radius {key.getKey}" data-alert="">
                        {key.getKey.toUpperCase} &mdash; {key.getValue}
                        <a href="#" class="close">&times;</a>
                    </div>
                </div>
            </div>
        }
    })

HTML OUTPUT:

    {"success": "The item has been created"}
    <div class="row">
       <div class="large-12 columns">
             <div class="alert-box radius {key.getKey}" data-alert="">
                  SUCCESS &mdash; The item has been created
                  <a href="#" class="close">&times;</a>
             </div>
        </div>
    </div>

使用@ variable

播放2.2.3 TEMPLATE(index.scala.html)
    @flash
    @(if (flash.size > 0) {
        flash.entrySet.iterator.map { key =>
            <div class="row">
                <div class="large-12 columns">
                    <div class="alert-box radius @{key.getKey}" data-alert="">
                        {key.getKey.toUpperCase} &mdash; {key.getValue}
                        <a href="#" class="close">&times;</a>
                    </div>
                </div>
            </div>
        }
    })

HTML OUTPUT:

    {"success": "The item has been created"}
    <div class="row">
       <div class="large-12 columns">
             <div class="alert-box radius @{key.getKey}" data-alert="">
                  SUCCESS &mdash; The item has been created
                  <a href="#" class="close">&times;</a>
             </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

编辑:由于目前还不清楚这是Scala还是Java,我已经修改了我使用Play Java的答案。

似乎Java中的模板工作略有不同,if语句周围的括号是搞乱的。你无论如何都不需要if语句,因为映射一个空的迭代器什么也不做。这有效:

@flash.entrySet.iterator.map { key =>
    <div class="row">
        <div class="large-12 columns">
            <div class="alert-box radius @{key.getKey}" data-alert="">
                @{key.getKey.toUpperCase} &mdash; @{key.getValue}
                <a href="#" class="close">&times;</a>
            </div>
        </div>
    </div>
}

如果你真的想要if

@if(flash.size > 0) {
     (above code)
}