Question

I have an issue with Play2 template engine. Some variables are not interpreted during the rendering process.

The variable {key.getKey} is not interpreted when it was surrended by double quote. How i can solve this problem ?

PLAY 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>

PLAY 2.2.3 TEMPLATE (index.scala.html) with @ variable

    @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>
Was it helpful?

Solution

EDIT: As it was unclear whether this was Scala or Java earlier, I've revised my answer to work with Play Java.

It appears that the templates work slightly different in Java, and the parenthesis around the if statement were messing things up. You don't really need the if statement anyway, as mapping an empty iterator will do nothing. 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(flash.size > 0) {
     (above code)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top