简体   繁体   中英

HowTo break out of a Tapestry loop?

So, i am trying to break out of tapestry loop here.

This is my -more or less- simplified scenario:

<ul>
    <t:loop source="firstSource" value="firstValue">
            <li>
                <t:loop source="firstValue" value="secondValue">
                    <p>${secondValue}</p>
                </t:loop>
                <t:loop source="secondSource" value="thirdValue">
                    <p>${thirdValue}</p>
                </t:loop>
            </li>
    </t:loop>
</ul>

What I do not want to have is: Tapestry loops through all entries in firstValue - then loops through all entries in secondSource. I do not want to iterate through secondSource inside the loop of fristValue as this would iterate through all entries in secondSource - and I just want to do 1 iteration at a time.

What I want to have is: Tapestry enters the loop for firstValue and does some printing or whatever, then breaks after the first iteration and jumps into secondSource to do the first iteration . After it has finished it jumps back to firstValue and repeats these steps. This is what in Java the "break;" would do.

I did not find a clue in the Tapestry documentation on how to do this, nor in their forums.

But it has to be possible in some way. I can not imagine I am the only one trying to do this.

Just put an if statement around the logic, probably using an index variable:

   <t:loop source="firstSource" value="firstValue">
            <li>
                <t:loop source="firstValue" value="secondValue" index="firstValueIndex">
                   <t:if test="firstCondition">
                     <p>${secondValue}</p>
                   </t:if>
                </t:loop>
                <t:loop source="secondSource" value="thirdValue">
                  <t:if test="secondCondition">
                    <p>${thirdValue}</p>
                  </t:if>
                </t:loop>
            </li>
    </t:loop>

In the Java page:

@Property
private int firstValueIndex;

public boolean getFirstCondition() {
  // logic to determine whether to break out
  return firstValueIndex == 0;
}

public boolean getSecondCondition() {
  // logic
}

My guess is that you have three sources of data and are trying to output three columns, is this right?

Sometimes you have to transform your data a little bit: For example, you might need to do some work to convert one value from each of the three inputs into a single value:

public class Row {  

  Object col1, col2, col2;

}

In your Java code, you would build up a List of Row objects.

In your template, you iterate over the Row objects, rendering the col1, col2 and col3 properties.

(In Tapestry 5.3 and above, a public field can be treated as a property.)

I've used similar techniques to output a calendar, which can be very tricky to manage using conditionals and the like inside the template.

Remember the role of the Controller in MVC: its job to mediate between the model and the view; sometimes that includes some simple transformations of the model data to fit in with the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM