Random Selection

steelej's Avatar

steelej

Jul 31, 2015 @ 02:02 PM

I have a page of tips for users. Is there a way in Velocity to randomly select one of those tips for display on another page and have it change each time the page is published?

  1. 1 Posted by Ryan Griffith on Jul 31, 2015 @ 06:58 PM

    Ryan Griffith's Avatar

    Hi Jim,

    You can use the Math Tool's random method to generate a random number. To summarize, you would:

    • obtain your array of tips using the XPath Tool or Locator Tool (depending on how you are accessing the structured data)
    • generate a random number (ie index)
      #set ( $random = $_MathTool.random(0 , $_MathTool.sub($tips.size(), 1)) )
      
    • get the tip at the random index
      $tips.get($random)
      

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by steelej on Jul 31, 2015 @ 08:49 PM

    steelej's Avatar

    Ryan,
    Thanks. I haven’t worked much with the math tool. This operates exactly as I wanted it to.

    Jim

  3. 3 Posted by Ryan Griffith on Jul 31, 2015 @ 08:51 PM

    Ryan Griffith's Avatar

    Not a problem at all, Jim, glad to hear the snippet helped out.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  4. Ryan Griffith closed this discussion on Jul 31, 2015 @ 08:51 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