XF 2.0 How to create arrays in a cycle within template?

CyberAP

Well-known member
It's possible to set arrays like this: <xf:set var="$myArr.1" value="1" />, but how do you do this N times?
 
Literally just a specific number of times?
HTML:
<xf:foreach loop="{{ range(1, 10) }}" value="$number">
   <xf:set var="{$myArr.{$number}}" value="{$number}" />
</xf:foreach>

{{ dump($myArr) }}
Should produce:
Code:
array:10 [▼
  1 => 1
  2 => 2
  3 => 3
  4 => 4
  5 => 5
  6 => 6
  7 => 7
  8 => 8
  9 => 9
  10 => 10
]
 
Admittedly, that solution might actually just be what you need to use rather than the approach you're using ;)
Code:
<xf:set var="$myArr" value="{{ range(1, 10) }}" />
Should result in roughly the same thing (perhaps a slight difference in the array keys).
 
This is almost perfect! I want to create an array of letters, this is the best way so far I've found with your solution:
HTML:
<xf:set var="$phrase" value="preview" />
<xf:set var="$strlen" value="{{strlen($phrase) - 1}}" />
<xf:foreach loop="{{ range(0, {$strlen}) }}" value="$number" i="$i">
    <xf:set var="$realI" value="{{$i - 1}}" />
    <xf:set var="{$phraseMatch.{$number}}" value="{$phrase.{$realI}}" />
</xf:foreach>
 
Back
Top Bottom