theTuxRacer
theTuxRacer

Reputation: 13869

Loop over two conditions in a template in play framework?

What is the way of doing this in the view template in playframework? I have tagged with groovy, because apparently, the play template engine is based on groovy.

%{ 
    for(int i=0, int j = 0; i < userdata.size(), j < user.size();i = i + 4, j++){
}%
<div style="text-align: center;">
<h3>
    ${foo.get(j)}
</h3>
</div>

If this is not possible, or just for curiosity sake:

I also tried passing foo as a hashmap, the keys of which are already present in userdata. I tried something like this but to no avail:

${foo.each{ k, v -> println "${k}:${v}" }}

Upvotes: 1

Views: 1958

Answers (2)

kheraud
kheraud

Reputation: 5288

It seams that you can't do this with play template builtin tags. Furthermore, I also have a compilation failure using for loop with multi-parameter initialization in a play 1.2.4 template. You can make it work with a while loop :

%{
    int j = 0, i = 0;
    while (i*i <= j) {
}%
        ${i}^2 <= ${j}
%{
        i++;
        j = j+2;
    }
}%
//prints
//0^2 <= 0
//1^2 <= 2
//2^2 <= 4

Upvotes: 1

aaberg
aaberg

Reputation: 2273

Since you are talking about groovy, I presume you are using playframework 1.x. Playframework 2 uses scala templates.

You can loop over two conditions, like you would do in any other language. The syntax is just a little different.

Java:

for (int i = 0; i < 10; i++){
    for (int j = 0; j < 10; j++) {
        System.out.println(String.format("i: %d, j: %d", i, j));
    }
}

playframework template:

#{list items:0..10, as:'i'}
    #{list items:0..10, as:'j'}
        <p>i: ${i}, j: ${j}</p>
    #{/list}
#{/list}

Check out the documentation for the #{list} tag

Upvotes: 2

Related Questions