2

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:aliasview-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.

Wouter J
  • 41,455
  • 15
  • 107
  • 112
Fidi
  • 5,754
  • 1
  • 18
  • 25

2 Answers2

2

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.

Fidi
  • 5,754
  • 1
  • 18
  • 25
  • I just had a similar problem and because of your answer here I noticed that mine also wasn't working because I didn't put additional single-quotes around one expression!! Thanks! :) You have to put them around the whole expression even if the braces are not around the entire expression like in my case: `arguments={"property:'additionalInfos.{i.index}.infoValue'"}` – Cold_Class Oct 23 '16 at 19:04
1

It should be {site:randomNumber(min: 1, max:10)}. Notice the casing. This is assuming, you have registered the namespace site like this at the beginning of your template:

{namespace site=Vendor\ExtName\ViewHelpers}

EDIT: The arguments must match the parameter names of the ViewHelper render function.

sven
  • 609
  • 5
  • 14
  • Of course the namespace is set. Otherwise the helper wouldn't have worked in the normal output ``. But I checked the casing. Obviously the casing doesn't make any difference: ` – Fidi Jul 28 '16 at 09:41