简体   繁体   English

如何将新变量添加到viewhelper(Typo3 9)

[英]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. 我创建了自己的viewhelper,以获取具有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. 如果我在viewhelper中打印数组,则会获得所有信息,但无法使用所有这些信息创建新的Fluid变量,无法在模板中使用。

I tried to create the new variable with: 我尝试使用以下方法创建新变量:

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

But I receive „Call to a member function assign() on null“. 但是我收到“在null上调用成员函数Assign()”。

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? 我该如何解决这个问题,才能获得流畅的访问体验?

You can use the templateVariableContainer for that: 您可以为此使用templateVariableContainer:

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

If you use the viewhelper in a loop, you'll have to remove the variable after render: 如果您在循环中使用viewhelper,则必须在渲染后删除该变量:

$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. 如果在模板中使用viewhelper,则变量将在流体模板中可用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM