Search code examples
tapestry

Looping a certain number of times in tapestry template


In a Tapestry component template, is there a simple way to render some markup X times, where X is a parameter of the component?

All I can find in the Tapestry docs is the Loop component:

<table class="navigation" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
    <tr>
        <t:loop source="pageNames" value="pageName">
            <td class="${tabClass}">
                <t:pagelink page="pageName">${pageName}</t:pagelink>
            </td>
        </t:loop>
    </tr>
</table>

But this is overkill if I just want to render something X times, without needing to pass any parameters. For this usecase I'd really expect something like (pseudo-code):

<table class="navigation" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
    <tr>
        <t:loop times="${x}">
            <!-- same markup every time -->
        </t:loop>
    </tr>
</table>

But nothing like this seems to exist - or does it?

For now my workaround is to provide a stub implementation of List which gives size X, and use that as my Loop source:

Class:

private int x;

public List<Object> getX() {
    return new AbstractList<Object>() {
        public Object get(int arg0) {
            return null;
        }
        public int size() {
            return x;
        }
    };
}

Template:

<t:loop source="x">
    <!-- same markup each time -->
</t:loop>

But this is very ugly - surely there's a nicer way to do something so straightforward?


Solution

  • This can be done with a loop and tapestry's range operator

    Santa Claus said: <t:loop t:source="1..3">Ho</t:loop>
    

    http://tapestry.apache.org/property-expressions.html