我有两个列表
cards=List[Card]
paymentMode=List[String]
我想同时在Twirl模板引擎(Play框架)中循环播放它们 我尝试过
(cards zip paymentMode).map{ case (card, p) =>
当我致电@card
时,我收到“找不到:储值卡”
答案 0 :(得分:1)
好吧,让我们先看一下Scala,然后看一下Twirl。因此,我们有两个列表:
scala> val cards: List[String] = List("a", "b", "c")
cards: List[String] = List(a, b, c)
scala> val paymentMethods: List[String] = List("visa", "master", "debit")
paymentMethods: List[String] = List(visa, master, debit)
然后将它们压缩在一起(我假设列表的长度相同):
scala> cards.zip(paymentMethods)
res0: List[(String, String)] = List((a,visa), (b,master), (c,debit)
在此之后,您可以通过一个for循环进行操作:
scala> for((c,p) <- res0){println(s"The card $c with payment $p")}
The card a with payment visa
The card b with payment master
The card c with payment debit
在“旋转”中:在您的视图中,您可以看到以下内容(请注意如何定义val并使用for循环以及如何使用@
作为转义符或作为参考):
@cards = @{List("a", "b", "c")}
@paymentMethods = @{List("visa", "master", "debit")}
@zipCardsPayments = @{cards.zip(paymentMethods)}
@for((c, p) <- zipCardsPayments){
<h3> card: @c with methods: @p</h3>
}
以下是我在浏览器中获得的输出: