1

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.

5 Answers 5

4
<f:for each="{0:1, 1:2, 2:3, 3:4, 4:5, 5:6, 6:7}" as="foo">{foo}</f:for>
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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

6 Comments

Hmm, i get The argument "map" was registered with type "array", but is of type "string" in view helper "TYPO3\CMS\Fluid\ViewHelpers\AliasViewHelper"
and this {1:1,2:2,3:3,4:4,5:5,6:6}? I only used this with words, in which case I work with a key ... we are talking v7.2.11 ?
@EdwardBlack great that you got it working, i'm not too sure if v6 is different in this case, otherwise its worth doing some more effort to get the 'simple' fluid expression to work ...
That would be a syntax error, yes. There are no versions of Fluid which support this expression.
<f:alias map="{numbers: {1,2,3,4,5,6}}"> is not valid Fluid syntax. You cannot define an array this way, and never could - maybe will, some day. The array you posted last, @webman, is properly associative and declares all keys, and thus is working. So it is not the same.
|
0

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

Comments

0

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.