ellei
ellei

Reputation: 73

TYPO3 returns "Array" to Fluid as String

I try to pass an array within my Viewhelper to the Fluidtemplate. It always shows the string "Array". If I try to use it as parameter in the f:for each viewhelper, I get an exception because it is a string and not an array. I used Typo3 6.2 before, now I have Typo3 7 and it stopped working.

public function render($uids) { // $uids='901,902,903'
    $uidArray = explode(',', $uids);

    $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
    $repository = $objectManager->get('XXX\\X\\Domain\\Repository\\FooRepository');
    $query = $repository->createQuery();
    $query->getQuerySettings()->setRespectStoragePage(FALSE);
    $query->matching(
        $query->in('uid', $uidArray)
    );
    return $query->execute()->toArray();
}

This is my Fluid template:

{namespace vh=My/Namespace/ViewHelpers}
<f:for each="{vh:GetArray(uids: '901,902,903')}">...</f:for>

Upvotes: 1

Views: 5139

Answers (2)

pgampe
pgampe

Reputation: 4578

You cannot return an array with your viewhelper, because viewhelper always return strings.

You can however introduce a new variable to the current render context and then use this variable inside your viewhelper.

public function render() {
  $returnArray = array('a' => 17, 'b' => 42);
  $this->templateVariableContainer->add('returnArray', $returnArray);
  $output = $this->renderChildren();
  $this->templateVariableContainer->remove('returnArray');
  return $output;
}

Inside your template you can then run a for loop over {returnArray}.

Upvotes: 3

Andrew
Andrew

Reputation: 605

Try a combination of f:for and f:cycle in your Fluid template. See the f:cycle examples in the Fluid ViewHelper Reference.

Upvotes: 0

Related Questions