[ 
https://issues.apache.org/jira/browse/FREEMARKER-101?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16560998#comment-16560998
 ] 

Ondra Žižka commented on FREEMARKER-101:
----------------------------------------

The reason is that sometimes, you want to randomize what is generated. For 
instance:

You have some data in the model, with some classification, and you use 
templates to render them.
For instance, you have tags as Set<String> and want to render a tag cloud.
To give it some spark, you want to render each tag with a different color.

Changing the incoming model to Map<String, String> or writing a custom function 
is, of course, also possibility.

Sometimes, people are in situation when changing the template is way easier 
than changing the Java app - for instance, when using an upstream project like 
JBake, they are only in charge of the templates, not the project. And getting 
such change to the project is for a long run.
I am not in that situation so personally I don't need it at the moment.

You're right that there should be limits of functionality :) 

Perhaps let's park the issue here (as deffered?) and see if there's other 
demand in the future.

 

 

> Add random number generation - perhaps 5.random()?
> --------------------------------------------------
>
>                 Key: FREEMARKER-101
>                 URL: https://issues.apache.org/jira/browse/FREEMARKER-101
>             Project: Apache Freemarker
>          Issue Type: New Feature
>            Reporter: Ondra Žižka
>            Priority: Major
>
> I can't find a way to easily create a random number in FreeMarker.
> Could we add it please?
> Something like:
> {code:java}
> 0?random() --> 0 to 0.9999999 like Math.random() 
> 0?random(5,9) --> random from that interval
> Or (incompatible with the logic above)
> 5?random() --> 0 to 5
> 5?random(-1) --> 0 to 4
> And if this is possible - to make it compatible with the two above:
> random() --> 0 to 0.9999999 like Math.random() 
> random(5,9) --> random from that interval{code}
> I am not quite sure about the overall consistency of the logic.
> Also, collections could have a built-in to pick random item:
> {code:java}
> myItems.random(){code}
> As a shorthand for
> {code:java}
> myItems[myItems.size().random(-1)]{code}
>  
> See here: 
> https://stackoverflow.com/questions/3680273/how-to-get-a-random-integer-in-freemarker-template/51554484#51554484



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to