Last active
January 9, 2024 12:45
-
-
Save vertexvaar/2ec9096a99a9e022a85442a311351c14 to your computer and use it in GitHub Desktop.
Backtracing ViewHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html data-namespace-typo3-fluid="true" | |
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" | |
xmlns:n="http://typo3.org/ns/CoStack/NextConf/ViewHelpers" | |
> | |
<n:touched> | |
<h1>Here it goes</h1> | |
<n:touch/> | |
<n:touched> | |
<h2>Not rendered</h2> | |
</n:touched> | |
<n:touched> | |
<h2>Rendered</h2> | |
<n:touch/> | |
</n:touched> | |
<n:touched> | |
<h2>Not rendered again</h2> | |
</n:touched> | |
</n:touched> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace CoStack\Backtracing\ViewHelpers; | |
use Closure; | |
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; | |
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; | |
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; | |
use function hash; | |
use function random_bytes; | |
class TouchedViewHelper extends AbstractViewHelper | |
{ | |
use CompileWithRenderStatic; | |
protected $escapeOutput = false; | |
protected $escapeChildren = false; | |
public static function renderStatic( | |
array $arguments, | |
Closure $renderChildrenClosure, | |
RenderingContextInterface $renderingContext | |
) { | |
$vhc = $renderingContext->getViewHelperVariableContainer(); | |
$parentIdentity = $vhc->get(self::class, 'identity'); | |
$identity = hash('sha1', random_bytes(16)); | |
$vhc->add(self::class, 'identity', $identity); | |
$vhc->add(self::class, 'touched-' . $identity, false); | |
$content = $renderChildrenClosure(); | |
$touched = $vhc->get(self::class, 'touched-' . $identity); | |
if (!$touched) { | |
$content = ''; | |
} | |
$vhc->add(self::class, 'identity', $parentIdentity); | |
return $content; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace CoStack\Backtracing\ViewHelpers; | |
use Closure; | |
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; | |
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; | |
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; | |
class TouchViewHelper extends AbstractViewHelper | |
{ | |
use CompileWithRenderStatic; | |
public static function renderStatic( | |
array $arguments, | |
Closure $renderChildrenClosure, | |
RenderingContextInterface $renderingContext | |
): void { | |
$vhc = $renderingContext->getViewHelperVariableContainer(); | |
$identity = $vhc->get(TouchedViewHelper::class, 'identity'); | |
$vhc->add(TouchedViewHelper::class, 'touched-' . $identity, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment