简体   繁体   English

从控制器调用 ViewHelper

[英]Call ViewHelper from Controller

i`m trying to call a ViewHelper Function from within a Controller in Typo3 (to add some additional header data)我正在尝试从 Typo3 中的控制器中调用 ViewHelper 函数(添加一些额外的标题数据)

Base is the Yag Gallery .基地是雅格画廊

I edited the ItemListController.php and added the following:我编辑了ItemListController.php并添加了以下内容:

$pager = $this->extListContext->getPagerCollection();
$prevLinkUid = $pager->getPreviousPage();
$arg = Tx_YAG_ViewHelpers_Namespace_GPArrayViewHelper::render([page:$prevLinkUid], $pager)
$test = '<link rel="test" href="' . $arg . '">';
$this->response->addAdditionalHeaderData($test);

The addAdditionalHeaderData function works well with other data (eg $prevLinkUid , so this part is functioning well). addAdditionalHeaderData 函数可以很好地处理其他数据(例如$prevLinkUid ,所以这部分运行良好)。

If i understand the syntax of GPArrayViewHelper::render correctly, i need a pageUid as first argument and the pagerCollection as second argument (derived from this call within Resources/Private/Partials/Pager/Default.html如果我正确理解了GPArrayViewHelper::render的语法,我需要一个 pageUid 作为第一个参数和 pagerCollection 作为第二个参数(派生自Resources/Private/Partials/Pager/Default.html 中的这个调用

 <extlist:link.action controller="{controller}" action="{action}" arguments="{extlist:namespace.GPArray(object:'{pagerCollection}' arguments:'page:{pager.previousPage}')}"><span>&lt;</span>       </extlist:link.action>

) )

However - if i try this Controller my page won't render, so i assume there is something wrong with the php code / function call, maybe even the syntax of the key value pair/first argument?但是 - 如果我尝试这个控制器,我的页面将不会呈现,所以我认为 php 代码/函数调用有问题,甚至键值对/第一个参数的语法都有问题? Sorry, i'm not a professional in php抱歉,我不是 php 专业人士

Any ideas how can i achieve this?任何想法我怎么能做到这一点? I read that it may be difficult to use ViewHelpers from within other controllers?我读到在其他控制器中使用 ViewHelpers 可能很困难?

Viewhelpers are not supposed to be used outside of Fluid templates.不应在 Fluid 模板之外使用 Viewhelper。

However, there might be an easier way to achieve what you want, eg with HeaderAssets .但是,可能有更简单的方法来实现您想要的,例如使用HeaderAssets This way you can easily add snippets to the <head> of your page from within a controller action or page template.通过这种方式,您可以轻松地从控制器操作或页面模板中将代码段添加到页面的<head>

Please really don't to that.请不要那样做。 What you can do is using the PageRenderer .您可以做的是使用PageRenderer Eg using例如使用

$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->addHeaderData($headerData);

there is a ViewHelperInvoker class which you can use to render a viewhelper in a controller:有一个 ViewHelperInvoker 类,您可以使用它在控制器中呈现一个视图助手:

    $viewHelperInvoker = GeneralUtility::makeInstance(\TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInvoker::class);
    $result = $viewHelperInvoker->invoke(
        \TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper::class,
        [ 'currencySign' => '€' ],
        new \TYPO3\CMS\Fluid\Core\Rendering\RenderingContext(),
        function() {
            return 12345.67;
        }
    );

https://github.com/TYPO3/Fluid/blob/master/src/Core/ViewHelper/ViewHelperInvoker.php https://github.com/TYPO3/Fluid/blob/master/src/Core/ViewHelper/ViewHelperInvoker.php

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

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