简体   繁体   English

无法在play2模板上解释变量

[英]Variable not interpreted on play2 template

I have an issue with Play2 template engine. 我对Play2模板引擎有问题。 Some variables are not interpreted during the rendering process. 在渲染过程中不会解释某些变量。

The variable {key.getKey} is not interpreted when it was surrended by double quote. 变量{key.getKey}用双引号引起来时不解释。 How i can solve this problem ? 我该如何解决这个问题?

PLAY 2.2.3 TEMPLATE (index.scala.html) PLAY 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: HTML输出:

    {"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>

PLAY 2.2.3 TEMPLATE (index.scala.html) with @ variable 使用@变量播放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: HTML输出:

    {"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>

EDIT: As it was unclear whether this was Scala or Java earlier, I've revised my answer to work with Play Java. 编辑:由于目前尚不清楚这是Scala还是Java,我已经修改了答案以使用Play Java。

It appears that the templates work slightly different in Java, and the parenthesis around the if statement were messing things up. 看起来模板在Java中的工作方式略有不同,并且if语句的括号使事情变得混乱。 You don't really need the if statement anyway, as mapping an empty iterator will do nothing. 无论如何,您实际上并不需要if语句,因为映射空的迭代器将无济于事。 This works: 这有效:

@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>
}

And if you really want the if : 如果您真的想要if

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM