freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alex Poruchikov (Jira)" <j...@apache.org>
Subject [jira] [Commented] (FREEMARKER-101) Add random number generation - perhaps 5.random()?
Date Sun, 06 Oct 2019 20:06:00 GMT

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

Alex Poruchikov commented on FREEMARKER-101:
--------------------------------------------

You can create a java class named {{TemplateHelper}}, for example.
This class should have a method like {{random(from, to)}}
Next - you pass a new instance of this class to the template context: r{{oot.put("helper",
new TemplateHelper())}}
And then in the template you call it: {{${helper.random(5,9)}}}

You can also shuffle collections and do any other things this way.

> 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
(v8.3.4#803005)

Mime
View raw message