I am not sure which framework you are using, but this is how you could do it using Scala´s native XML library:
<div>
{ for (row <- months grouped 3) yield (<tr> { for (month <- row) yield (<td> {month} </td>) } </tr>) }
</div>
Rendered slightly more prettily, the code looks like this:
for (row <- months grouped 3) yield {
<tr>
{ for (month <- row) yield (<td> {month} </td>) }
</tr>
}
Which hopefully makes the structure clear.
If your framework works with yield, all you need to do is add @ in the appropriate places. If it does not, you might have to do something like this:
for (row <- @months grouped 3) {
<tr>
for (month <- @row) {
<td> @month </td>
}
</tr>
}
Your code is structured only around months, so has to use conditional logic to create your rows. My code is structured around rows (and within that around months), so needs no conditionals. My code also only has the value for the number of months in one place, which is less error prone and makes it easier to change the number of months in a row.
You don't have to use a list comprehension, though. The important thing is simply to slice the list up into groups of 3 (or n
, where n
is the number of months you want in each row) and iterate over those.
<td>Jan</td><td>Feb</td><tr><td>Mar</td></tr>
and not<tr><td>Jan</td><td>Feb</td><td>Mar</td></tr>
. I've assumed the latter is what you actually intended. \$\endgroup\$