In one of my NEOS Templates I try to solve the simple task of generating a random number (within a specified range) and store it into a variable for later usage.
Since none of the default view helpers offers such a feature I developed my own view helper, which expects a min and max value. Internally the view helper uses php's rand($min, $max)
.
The following example is working in my template:
site:RandomNumber(0, 17)
As expected this outputs a random number. However now I need to store the result into a variable, since I have to use it more than one time.
I came across fluids alias-view-helper:
<f:alias map="{number: 33}">
The number is {number}
</f:alias>
This results in:
The number is 33
Now I want the number not to be 33, but the result of my RandomNumber
-view-helper.
I tried things like:
<f:alias map="{number: {site:RandomNumber(0, 17)}}">
The number is {number}
</f:alias>
This however throws an Exception saying:
The argument "map" was registered with type "array", but is of type "string"
in view helper "TYPO3\Fluid\ViewHelpers\AliasViewHelper"
The docs of the f:alias
view-helper say accepted values are other view-helpers, but they never give any examples on that.
Am I completely wrong with this approach? Is it simply not possible to assign a simple variable within a fluid-template?
Further information: I do have a slider on the website, which should start with a different slide on (almost) every page-load. So I need to grab this random slide-number, which I have to refer to in the slider markup several times.
I digged into it again and first tried to output:
{site:randomNumber(0,17)} <- was output as the string, not the expected result
<site:randomNumber min="0" max="17" /> <- this was the expected output
The first one, is the one I needed to get to work to use it in the alias-helper right?
So I first had to ensure, that this first one works!
I randomly guessed that it's necessary to specify the argument-names. So I tried this:
{site:randomNumber({min: 0, max: 17})}
Coming from PHP I thought, that giving an array with the arguments was the solution. However I was wrong.
Googling for "fluid inline notation" lead me to this resource: https://wiki.typo3.org/Fluid_Inline_Notation
There I saw, that I was very close. The arguments have to be given by their names, but not in array-notation, so THIS produced the expected output:
{site:randomNumber(min: 0, max: 17)}
So I got one step further to the solution. So I took this snippet and pasted it into the alias-helper like this:
<f:alias map="{number: {site:randomNumber(min: 0, max: 17)}}">
The number is {number}
</f:alias>
This however lead to the same exception as before. I felt I was close, so guessed in wrapping the expression into single-quotes, like:
<f:alias map="{number: '{site:randomNumber(min: 0, max: 17)}'}">
The number is {number}
</f:alias>
That's everything I wanted. Hard to believe that one needs 2 days to figure this out, since documentation is really bad.