FluidTYPO3 / vhs

TYPO3 extension VHS: Fluid ViewHelpers
https://fluidtypo3.org
Other
189 stars 228 forks source link

About v:iterator.shift #1734

Closed typo3ua closed 3 years ago

typo3ua commented 3 years ago

I have array

array(3 items)
   0 => ''
   1 => 'Item 1'
   2 => 'Item 2'

I need to delete 0 => '' I added {array -> v:iterator.shift(as: 'foo') -> v:variable.set(name: 'too')} ...and I get the same

array(3 items)
   0 => ''
   1 => 'Item 1'
   2 => 'Item 2'

What is wrong?

tantegerda1 commented 3 years ago

The iterator.shift viewhelper does not change the input data, it merely returns the value of the first element.

https://github.com/FluidTYPO3/vhs/blob/development/Classes/ViewHelpers/Iterator/ShiftViewHelper.php#L17

Shifts the first value off $subject (but does not change $subject itself as array_shift would).

If I'm correct, in your code the variable foo should hold the value of the first array element, i.e. an empty string '', whereas the text output of the whole operation (none, an empty string again) is set in the variable too. The variable array is unchanged all the time.

You might want to have a look at iterator.slice.

typo3ua commented 3 years ago

Hello, @tantegerda1

You might want to have a look at iterator.slice.

Thank you for reply... can you give me an example

typo3ua commented 3 years ago
{v:variable.set(name: 'arr', value: '{0: \'\', 1: \'Item 1\', 2: \'Item 2\'}')}
{v:iterator.slice(haystack: '{arr}', start: 1, length: 2) -> v:variable.set(name: 'foo')}

Thanks! I did it...