Any thoughts on how to generate a random number with velocity?
Community moderators have prevented the ability to post new answers.
OK, how about this. A simple pseudo-unique value for multiple macros on the same page is the timestamp at which they were rendered. This will always be unique unless:
Here's my example user macro:
## @noparams <div> <strong>Unique Value:</strong> <em> $action.dateFormatter.calendar.timeInMillis </em> </div>
And what it looks like with two of them of the page:
I had thought about soem knd of timestamp ... that was going to be my next question if the random number thing didn't pan out. ;) If it goes down to the miliseconds that should probably be unique enough. I'll give that a shot. Thanks sir.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Much better to not put "business logic" into the templating engine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I disagree. This is for a user macro that is going to have proceeding javascript. I need to be able to uniquely identify the html created by the macro just incase the user macro is added multiple times to the page. A sufficently large random number seemed to me to be the easiest way to get a unique number ... since the indvidual macros will not be aware of the state of the others. Unless you can think of some other way to get a unique identifier to put on the html element.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Unfortunately the Velocity language doesn't let you invoke static methods on classes that cannot be constructed (eg. java.util.Math), so it's not possible to get a velocity template to generate a random number.
You can work around this by having Java code on the server side that populates the Velocity context with the information it needs, but user macros do not allow macro authors to embed Java code in the system, so it's not possible in this case.
This SO question has more info: http://stackoverflow.com/questions/2329191/calling-class-methods-static-from-inside-a-velocity-view-page
Can you use the Javascript side of your macro to generate the random number? Alternatively you may need to add a parameter to the user macro that requires the author to manually supply a random/unique value.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Adding the random number via javascript would not be useful. The whole pont was so that I could put a unique id on an html element and them target that with the javascript contained in the user macro. Otherwise if the uer adds this user macro multiple times to a page then the JS in each would affect all of them. I'm trying to get a unique value that I can put in the element and pass to the JS, so that the scripting is targeted to that specific user macro. Does that make sense. The randome number thing was just a thought on how I might get a unique-ish value ... if the random number was sufficently large. If there is another way to get a unique value that i can put in the html and JS then I would be all for that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.