Martin
Martin

Reputation: 83

TYPO3: Generate random variable in FLUID Template

I have three images in my FLUID template and would like to show one of these three images at random. Is there any method to do this in the FLUID template or is this impossible and I must go a other way?

I tried the TYPO3 extension "VHS" to generate a random number:

{v:random.number(minimum: 1, maximum: 3, minimumDecimals: 0, maximumDecimals: 0)}

In the frontend I see the generated number. But how can I set the random number to a variable to use it in a if-else-condition?

Upvotes: 2

Views: 2775

Answers (3)

Yurii P.
Yurii P.

Reputation: 61

I do not use vhs in TYPO3v9. I use seconds and simple math. I get the random value of the iterator from the array through:

<f:variable name="sec4random" value="{f:format.date(date:'0 seconds', format:'s')}"/>
<f:variable name="count4random" value="{f:count(subject: images)}"/>
Random index for iterator: {sec4random % count4random} 

Upvotes: 2

bschauer
bschauer

Reputation: 1008

This would also be possible with VHS

<v:iterator.sort subject="{images}" as="randomimages" order="RAND" />

Upvotes: 5

Claus Due
Claus Due

Reputation: 4271

On TYPO3v7 with VHS installed:

{v:random.number(...) -> v:variable.set(name: 'newvariablename')}

On TYPO3v8 and above, or on Fluid standalone:

{v:random.number(...) -> f:variable(name: 'newvariablename')}

Then use the new variable:

<f:if condition="{newvariablename}" then="random number greater than zero" />

Upvotes: 4

Related Questions