Black
Black

Reputation: 20362

How to define array of numbers and output it?

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.

Upvotes: 1

Views: 2040

Answers (5)

webman
webman

Reputation: 1203

try this:

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

corrected after Clause Due remarks ...

Upvotes: 1

ESP32
ESP32

Reputation: 8723

Based on @webman's answer I tried the following in Typo3 V11 sucessully:

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

Upvotes: 0

Claus Due
Claus Due

Reputation: 4271

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.

Upvotes: 2

V-K
V-K

Reputation: 1357

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

Upvotes: 4

Black
Black

Reputation: 20362

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

Upvotes: 0

Related Questions