Labs ICT
Pro Login

Loop Status Variables

index, count, size, even, odd in loops.

Loop Status Variables

When you use th:each, Thymeleaf gives you a status object that contains useful information about the current iteration. This is super handy for styling and logic.


  • 0. Item
  • The status object is the second parameter in th:each. You can name it anything you want.

    status.index and status.count

    status.index gives you the zero-based index, while status.count gives you the one-based count.

    
    
      0
      1
      User Name
    
        

    Use index when you need array-like behavior, and count when you want to show "Item 1 of 5" style numbers.

    status.size

    The status.size property tells you how many elements are in the collection being iterated.

    
    

    Showing item 1 of 5

    This is great for showing progress indicators or "Page 1 of 10" type displays.

    Try it Yourself →

    status.first and status.last

    These boolean properties are true when you're on the first or last iteration respectively.

    
    
  • Item ,
  • Perfect for adding commas between items but not after the last one.

    status.even and status.odd

    These properties let you apply alternating styles, like zebra-striping in tables.

    
    
      Item
    
        

    status.odd is true for iterations 1, 3, 5, etc., and status.even for 2, 4, 6, etc.

    Custom Variable Name

    You can name the status variable whatever you want. Just make sure it's meaningful.

    
    
    1. Product

    Using short names like i, iter, or st keeps your code concise.

    Try it Yourself →

    Combining Status Properties

    You can combine multiple status properties for complex logic and styling.

    
    
    Item

    This creates an indentation effect where each item is pushed further to the right.