Is there a way to randomize an output with velocity

Thane Webb's Avatar

Thane Webb

08 Jul, 2015 04:02 PM

We currently have a quote that displays on our homepage that is setup with start and end dates. So once the quote expires the next quote takes it's place. We are finding that this doesn't change out as much as we would like. We would like to display a randomized quote from the set entered on the page. I know this can be done using javascript or php but is it possible in velocity? I would prefer to do this in velocity so only one quote is actually output rather than outputting all the quotes and then randomizing which displays with either php or javascript.

  1. 1 Posted by Ryan Griffith on 08 Jul, 2015 05:23 PM

    Ryan Griffith's Avatar

    Hi Thane,

    You should be able to accomplish this by first obtaining an array of your quotes (eg using the XPath Tool) and then use the Math Tool's random method to generate a random number so you can access that index within your array.

    For example, consider the following pseudo code:

    #set ( $quotes = $_XPathTool.selectNodes($contentRoot, "//quote") )
    #set ( $random = $_MathTool.random(0, $_MathTool.sub($quotes.size(), 1)) )
    #set ( $quote = $quotes.get($random) )

    Please let me know if you have any questions.


  2. Tim closed this discussion on 03 Aug, 2015 08:07 PM.

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

Keyboard shortcuts


? 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