Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Static rendering of Htmlentities ViewHelper
<?php
namespace TYPO3\Fluid\ViewHelpers\Format;
/* *
* This script belongs to the TYPO3 Flow package "Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\Fluid\Core\ViewHelper\Facets\CompilableInterface;
/**
* Applies htmlentities() escaping to a value
* @see http://www.php.net/manual/function.htmlentities.php
*
* = Examples =
*
* <code title="default notation">
* <f:format.htmlentities>{text}</f:format.htmlentities>
* </code>
* <output>
* Text with & " ' < > * replaced by HTML entities (htmlentities applied).
* </output>
*
* <code title="inline notation">
* {text -> f:format.htmlentities(encoding: 'ISO-8859-1')}
* </code>
* <output>
* Text with & " ' < > * replaced by HTML entities (htmlentities applied).
* </output>
*
* @api
*/
class HtmlentitiesViewHelper extends AbstractViewHelper implements CompilableInterface {
/**
* Disable the escaping interceptor because otherwise the child nodes would be escaped before this view helper
* can decode the text's entities.
*
* @var boolean
*/
protected $escapingInterceptorEnabled = FALSE;
/**
* Escapes special characters with their escaped counterparts as needed using PHPs htmlentities() function.
* Note: If the value is NULL the child nodes are used as input. If the input is not a string, the unmodified value is returned
* This ViewHelper is rendered statically. @see renderStatic()
*
* @param string $value string to format
* @param boolean $keepQuotes if TRUE, single and double quotes won't be replaced (sets ENT_NOQUOTES flag)
* @param string $encoding
* @param boolean $doubleEncode If FALSE existing html entities won't be encoded, the default is to convert everything.
* @return string the altered string
* @see http://www.php.net/manual/function.htmlentities.php
* @api
*/
public function render($value = NULL, $keepQuotes = FALSE, $encoding = 'UTF-8', $doubleEncode = TRUE) {
return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
}
/**
* Applies htmlentities() on the specified value.
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param \TYPO3\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
* @return mixed
* @see \TYPO3\Fluid\Core\ViewHelper\Facets\CompilableInterface
*/
static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
$value = $arguments['value'];
if ($value === NULL) {
$value = $renderChildrenClosure();
}
if (!is_string($value)) {
return $value;
}
$flags = $arguments['keepQuotes'] ? ENT_NOQUOTES : ENT_COMPAT;
return htmlentities($value, $flags, $arguments['encoding'], $arguments['doubleEncode']);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment