Velocity foreach count, even/odd

Ashley Williams's Avatar

Ashley Williams

15 Jan, 2014 08:26 PM

Hello,

I have two sets of images (one is white, the other is red) that I need to alternate between in a foreach loop. Is there a way that Velocity can do this, maybe by display one image if the foreach count is odd, and the other image if it is even? Thanks in advance.

  1. 1 Posted by Ryan Griffith on 15 Jan, 2014 10:08 PM

    Ryan Griffith's Avatar

    Hi Ashley,

    You should be able to accomplish this by using the $_MathTool.mod() method and $foreach.index. Try something like the following and let me know how it works out:

    #foreach(...)
        #set ($imageColor = "white")
        #if ($_MathTool.mod($foreach.index, 2) == 1)
            #set ($imageColor = "red")
        #end
    
        ## $imageColor will be "white" for even and "red" for odd
    #end
    

    Note: depending on your use case, you may nee to switch the two colors around.

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by Ashley Williams on 15 Jan, 2014 10:20 PM

    Ashley Williams's Avatar

    That's just what I needed, thank you!

  3. Ashley Williams closed this discussion on 15 Jan, 2014 10:20 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM