{textWithHtml} * ``` * * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-format-htmlentities * @see https://www.php.net/manual/function.htmlentities.php */ final class HtmlentitiesViewHelper extends AbstractEncodingViewHelper { /** * Output gets encoded by this viewhelper * * @var bool */ protected $escapeOutput = false; /** * This prevents double encoding as the whole output gets encoded at the end * * @var bool */ protected $escapeChildren = false; public function initializeArguments(): void { $this->registerArgument('value', 'string', 'string to format'); $this->registerArgument('keepQuotes', 'bool', 'If TRUE, single and double quotes won\'t be replaced (sets ENT_NOQUOTES flag).', false, false); $this->registerArgument('encoding', 'string', 'Define the encoding used when converting characters (Default: UTF-8'); $this->registerArgument('doubleEncode', 'bool', 'If FALSE existing html entities won\'t be encoded, the default is to convert everything.', false, true); } /** * Escapes special characters with their escaped counterparts as needed using PHPs htmlentities() function. * * @see https://www.php.net/manual/function.htmlentities.php */ public function render(): mixed { $value = $this->renderChildren(); $encoding = $this->arguments['encoding']; $keepQuotes = $this->arguments['keepQuotes']; $doubleEncode = $this->arguments['doubleEncode']; if (!is_string($value) && !(is_object($value) && method_exists($value, '__toString'))) { return $value; } if ($encoding === null) { $encoding = self::resolveDefaultEncoding(); } $flags = $keepQuotes ? ENT_NOQUOTES : ENT_QUOTES; return htmlentities((string)$value, $flags, $encoding, $doubleEncode); } /** * Explicitly set argument name to be used as content. */ public function getContentArgumentName(): string { return 'value'; } }