templatesscalaplayframework-2.0

How to loop over the flash elements in a Scala template?


I'm having a hard time trying to translate a bit of template from 1.2.4 to 2.0.

So far, I managed to loop through all the flash elements, but I'd like to get the Key and the Message separately (@msgKey contains a list, and I don't know how to split it :/) => (success, Your data has been updated).

Here's the original code:

#{if flash.data.size() > 0}
    #{list items:flash.data, as:'msg'}
        #{if msg.key.substring(0, 4).equals('info')}#{set msg_type:'info' /}#{/if}
        #{if msg.key.substring(0, 4).equals('succ')}#{set msg_type:'success' /}#{/if}
        #{if msg.key.substring(0, 4).equals('warn')}#{set msg_type:'warning' /}#{/if}
        #{if msg.key.substring(0, 4).equals('erro')}#{set msg_type:'error' /}#{/if}
        <div class="alert alert-${msg_type}" data-dismiss="alert">  
            <a title="Close that message" class="close">×</a>
            ${msg.value.raw()}
        </div>
    #{/list}
#{/if}

And here's the new one :

@if(!flash.isEmpty()) {
    @for(msgKey <- flash) { 
        <div class="alert alert-@msgKey" data-dismiss="alert">      
            <a title="@Messages("misc.message.close")" class="close">×</a>
            @msgKey
        </div>
    }
}

Solution

  • Besides Marius solution, you can also write this:

    @if(!flash.isEmpty()) {
        @for((msgKey, msgValue) <- flash) { 
            <div class="alert alert-@msgKey" data-dismiss="alert">      
                <a title="@Messages("misc.message.close")" class="close">×</a>
                @msgKey
            </div>
        }
    }