Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

A little bit of help would be greatly appreciated! :)

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

Thanks for your help, I appreciate!

like image 515
Cyril N. Avatar asked Dec 28 '22 02:12

Cyril N.


1 Answers

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>
    }
}
like image 148
Daniel C. Sobral Avatar answered Jan 14 '23 15:01

Daniel C. Sobral