Search code examples
randomtypo3fluidview-helpers

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?


Solution

  • 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" />