Variable inside scala template in playback structure

I need to be able to declare variables and after some markup later I need to reference them. To do this, this is a simplified version of my scala template:

@(map1: java.util.LinkedHashMap[String,java.util.LinkedHashMap[String,Object]]) @import scala.collection.JavaConversions._ @import play.Logger @for( (key,value) <- map1) { <div> @{ val rmap = Foo.someMethod(value) val baz = rmap.getOrElse("baz", null) <table border="0" cellpadding="0" cellspacing="0" > <tbody> <tr> <td rowspan="3"> <div class="bar"> @baz </div> </td> </tr> </tbody> </table> } </div> } 

Is above a valid scala pattern, and if not, how can I declare baz and reference it later in the markup? I am using 1.2.2RC2 and scala 0.9.1

+4
source share
2 answers

I was curious, so some dug. See https://groups.google.com/forum/#!topic/play-framework/Mo8hl5I0tBQ - at the moment there is no way, but interesting work is shown. Define utils / Let.scala:

 package utils Object Let { def let[A,B](a:A)(f:A=>B):B = f(a) } 

and then

 @import utils.Let._ @let(2+3){ answer => @answer <hr> @answer } 

This is a very functional way of processing, but then what did you expect in Scala :)

+8
source

You can simply use to understand:

 @for( (key,value) <- map1; rmap = Foo.someMethod(value); baz = rmap.getOrElse("baz", null) ) { <div> <table border="0" cellpadding="0" cellspacing="0" > <tbody> <tr> <td rowspan="3"> <div class="bar"> @baz </div> </td> </tr> </tbody> </table> </div> } 

... and if you don't have something you need to iterate over, you can just say @for(i <- List(1); <declare variables>){<html here>}

+3
source

All Articles