Search code examples
javapebble

Pebble Template loops


I am trying to build a simple loop where I execute the template N times. I wrote a function that accepts a parameter and outputs string based on it and I need to execute it a bunch of times.

Following works if I explicitly define a range:

{% for t in ["0","1","2"] %}
{{ customFunction(t) }}
{% endfor %}

However I want something like loop over some arbitrary range (or even a while loop where I execute a custom function N times):

{% for t in [0..15] %}
{{ customFunction(t) }}
{% endfor %}

I also tried defining a function that returns a range ["0","1","2"] called range() and tried using in the for-loop with no luck:

{% for t in range() %}
{{ customFunction(t) }}
{% endfor %}

So not sure if this is possible.


Solution

  • AlexC. I also had a headache with that.

    I found a solution hoping it corresponding to your issue. You were almost there!

    In pebble template, to use simple loop with for statement, use code as below.

    {% set n = 15 %}
    {% for t in range(1, n) %}
    {{ customFunction(t) }}
    {% endfor %}
    

    FYI, Below is actual applied in my code where totalPageCount is from spring model value(primitive integer).

    {% for i in range(1, totalPageCount) %}
    <pre>
    <li><a href="">{{ i }}</a></li>
    </pre>
    {% endfor %}
    

    Hopefully you would make it work!