Cyril N.
Cyril N.

Reputation: 39859

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

Upvotes: 4

Views: 791

Answers (2)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297155

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

Upvotes: 6

Marius Soutier
Marius Soutier

Reputation: 11274

The data in the flash is a Map, iterating over it yields a tuple of two elements, the key and the value. You can access the key with @msgKey._1 and the value with @msgKey._2.

Upvotes: 4

Related Questions