Bug #94125

Fluid f:render with argument "contentAs" does not work as expected

Added by Gabriel Kaufmann / Typoworx NewMedia 5 months ago. Updated 5 months ago.

Status:
New
Priority:
Should have
Assignee:
-
Category:
Fluid
Start date:
2021-05-12
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
9
PHP Version:
Tags:
Complexity:
Is Regression:
Sprint Focus:

Description

Fluid f:render with argument "contentAs" does not work as expected, as the documentation implies a delegation of the rendered content is always possible to use pre-rendered content in the given variable to output the contents using variable.

But this seems to be broken, as the variable is only set by using another rendering call or a delegate-class. There is no implicit call to VariableContainer setting the variable without these cases.

As known from other ViewHelpers a given "as" argument will return the content/result into that variable instead of implicit rendering.

Usage cases
would be to render a given wrapping HTML only in case the f:render will return a non-empty result.

Code Reference
https://github.com/TYPO3/Fluid/blob/fb42c02cb286f99ac677679f03280b2ad2d2b54e/src/ViewHelpers/RenderViewHelper.php#L145

Feature should be interesting for TYPO3 v9.5 upwards


Files

RenderViewHelper.php.issue-94125.patch (1.06 KB) RenderViewHelper.php.issue-94125.patch Patch for TYPO3Fluid\Fluid\ViewHelpers\RenderViewHelper Gabriel Kaufmann / Typoworx NewMedia, 2021-05-12 12:46
#1

Updated by Gabriel Kaufmann / Typoworx NewMedia 5 months ago

I've made the following xclass-override which resolves my proposal:

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

class RenderViewHelper extends \TYPO3Fluid\Fluid\ViewHelpers\RenderViewHelper
{
    /**
     * @return mixed
     */
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
    {
        $content = parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);

        if(!empty($arguments['contentAs']))
        {
            $renderingContext->getVariableProvider()->add($arguments['contentAs'], $content);

            if(($content = $renderChildrenClosure()) !== NULL)
            {
                $renderingContext->getVariableProvider()->remove($arguments['contentAs']);
            }
        }

        return $content;
    }
}

And the following patch should solve this in the original class.

This would allow such cases rendering optional wraps only if content exists:

<f:render section="Footer" contentAs="footerSection">
  <div class="footer--content">
     {footerSection -> f:format.raw()}
  </div>
</f:render>

Also available in: Atom PDF