0
votes

How can I define an array of numbers and output each number in a for loop?

I tried it like this:

  <f:alias map="{numbers: [1,2,3,4,5,6]}">
    <f:for each="{numbers}" as="number">
      <p>{number}</p>
    </f:for>
  </f:alias>

Result:

The argument "map" was registered with type "array", but is of type "string" in view helper "TYPO3\CMS\Fluid\ViewHelpers\AliasViewHelper"

And like this:

  <f:alias map="{v:iterator.explode(content: '1,2,3,4,5,6')}">
      <f:for each="{content}" as="zahl">
          <p>{zahl}</p>
      </f:for>
  </f:alias>

Result: No output.

4

4 Answers

4
votes
<f:for each="{0:1, 1:2, 2:3, 3:4, 4:5, 5:6, 6:7}" as="foo">{foo}</f:for>
1
votes

The ideal solution IF and only IF:

  • You use VHS.
  • You want numbers starting from 0 or 1 going to a max; or numbers calculated using those two starting indices (TYPO3 8.0+ supports math expressions in Fluid natively, earlier versions require VHS for this).
  • You want to loop the numbers, not consume them as an array.

Which seems to be exactly your use case...

Then, and only then, is the following the ideal solution in terms of both performance and minimising complexity:

<v:iterator.loop count="6" iteration="iteration">
{iteration.index} starts at zero, {iteration.cycle} starts at one.
</v:iterator.loop>

Don't forget the following either:

{f:render(section: 'OtherSection', arguments: {iteration: iteration})
    -> v:iterator.loop(count: 6, iteration: 'iteration')}

Which is the most efficient way of rendering a section X number of times with only the iteration variable being different. Sections or partials are the most efficient way to represent this exact type of code and the inline syntax is the most efficient when parsing.

0
votes

try this:

 <f:alias map="{numbers: {1,2,3,4,5,6}}">
    <f:for each="{numbers}" as="number">
      <p>{number}</p>
    </f:for>
 </f:alias>
0
votes

I was able to solve it with this code:

<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
      xmlns:v="http://typo3.org/ns/FluidTYPO3/Vhs/ViewHelpers"
      v:schemaLocation="https://fluidtypo3.org/schemas/vhs-master.xsd">

<f:for each="{v:iterator.explode(content: '1,2,3,4,5,6')}" as="number">
   <p>{number}</p>
</f:for>

Output:

1
2
3
4
5
6
  1. Make sure that you have installed the extension VHS: Fluid ViewHelpers (extension key = vhs).
  2. Make sure to include the extension in the partial.

Write this at the top:

<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
      xmlns:v="http://typo3.org/ns/FluidTYPO3/Vhs/ViewHelpers"
      v:schemaLocation="https://fluidtypo3.org/schemas/vhs-master.xsd">

I am using Typo3 v6.2.25