ViewHelpers in Extbase

Two types of ViewHelpers in Extbase

1. Conditional ViewHelper example

<?php

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

class IsNumericViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper
{
    public function initializeArguments()
    {
        //$this->registerArgument($name, $type, $description, $required, $defaultValue)
        $this->registerArgument('check', 'string', 'The string to check', true);
    }

    /**
     * @param array $arguments
     * @param RenderingContextInterface $renderingContext
     *
     * @return bool true or false
     */
    public static function verdict(
        array $arguments,
        RenderingContextInterface $renderingContext
    ) {
        return is_numeric($arguments['check']);
    }
}

2. Simple ViewHelper example

<?php

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

class SampleViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper
{
    use CompileWithRenderStatic;

    /**
     * Escape output
     * @var bool
     */
    protected $escapeOutput = false;

    public function initializeArguments()
    {
        //$this->registerArgument($name, $type, $description, $required, $defaultValue)
        $this->registerArgument('argument', 'object', 'The category to check', true);
    }

    /**
     * @param array $arguments
     * @param \Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     */
    public static function renderStatic(
        array $arguments,
        \Closure $renderChildrenClosure,
        RenderingContextInterface $renderingContext
    ) {
        // Your logic here. Should return string
        return '';
    }
}