What is the best way to make a loop in ISML without iterable object?

2.7k Views Asked by At

I want to make a loop in my ISML Template without an iterable object. During the runtime of this template the condition or rather the number of iterations would be defined. Is there any possibility to have a loop statemant like in java "for (int i = 0; i < 5; i++)" but without complex java code?

1

There are 1 best solutions below

0
On

There is no truly elegant way, I believe. That's because such calculations do not belong to the view layer. That's true not only for ISML but for other template engines, e.g. Thymeleaf. See here.

ISLOOP requires one of the following standard java instances in iterator:

java.util.Enumeration
java.util.Iterator
java.util.Collection

E.g.:

<isloop iterator="products" alias="product" counter="c">

</isloop>

The control flow within the loop may be changed with isbreak and isnext:

<isloop
iterator = "{ISML variable identifier}"
[ alias = "{simple name}" ]
[ counter = "{counter name}" ]
>
... some HTML and ISML code ...
[<isnext>]
[<isbreak>]
</isloop>

If you really need that you may create, for example, your own iterator as simple as this and put it in the pipeline dictionary from a pipeline or an ISML module:

class MyIterator implements Iterator<Integer>
{
    private final int max;
    private int current;

    MyIterator(int max)
    {
        this.max = max;
    }

    @Override
    public boolean hasNext()
    {
        return current < max;
    }

    @Override
    public Integer next()
    {
        return current++;
    }
}

You may also use plain JSP scriptlet embedded in the ISML, ISML module etc. If you need a more specific answer please provide more context in your question.