A.Tietz
A.Tietz

Reputation: 77

How to add a new variable to a viewhelper (Typo3 9)

I created my own viewhelper for getting all Information of an Image with the UID. I get all the Information, if I print the array in the viewhelper, but im not able to create a new Fluid variable with all these information, to work with in the Template.

I tried to create the new variable with:

$this->view->assign('sliderItems', $sliderItems);

But I receive „Call to a member function assign() on null“.

I render the Image with:

public function render() {
    /* MY RENDER STUFF */

And then I will submit the array with:

    $this->view->assign('sliderItems', $sliderItems);
}

How can I solve this, to get an access with fluid?

Upvotes: 2

Views: 1343

Answers (1)

RinyVT
RinyVT

Reputation: 326

You can use the templateVariableContainer for that:

$this->templateVariableContainer->add('key', 'value');

If you use the viewhelper in a loop, you'll have to remove the variable after render:

$this->templateVariableContainer->add('key', 'value');
$output = $this->renderChildren();
$this->templateVariableContainer->remove('key');
return $output;

If you use your viewhelper in your template, the variable will be available in your fluid template.

<vendor:viewhelper>
{key}
</vendor:viewhelper>

Upvotes: 3

Related Questions