a8727c591e7577cbf559793312830b33b5823bbe
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / HtmlentitiesViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * Applies htmlentities() escaping to a value
16 * @see http://www.php.net/manual/function.htmlentities.php
17 *
18 * = Examples =
19 *
20 * <code title="default notation">
21 * <f:format.htmlentities>{text}</f:format.htmlentities>
22 * </code>
23 * <output>
24 * Text with & " ' < > * replaced by HTML entities (htmlentities applied).
25 * </output>
26 *
27 * <code title="inline notation">
28 * {text -> f:format.htmlentities(encoding: 'ISO-8859-1')}
29 * </code>
30 * <output>
31 * Text with & " ' < > * replaced by HTML entities (htmlentities applied).
32 * </output>
33 *
34 * @api
35 */
36 class HtmlentitiesViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Format\AbstractEncodingViewHelper implements \TYPO3\CMS\Core\SingletonInterface {
37
38 /**
39 * Disable the escaping interceptor because otherwise the child nodes would be escaped before this view helper
40 * can decode the text's entities.
41 *
42 * @var bool
43 */
44 protected $escapingInterceptorEnabled = FALSE;
45
46 /**
47 * Escapes special characters with their escaped counterparts as needed using PHPs htmlentities() function.
48 *
49 * @param string $value string to format
50 * @param bool $keepQuotes if TRUE, single and double quotes won't be replaced (sets ENT_NOQUOTES flag)
51 * @param string $encoding
52 * @param bool $doubleEncode If FALSE existing html entities won't be encoded, the default is to convert everything.
53 * @return string the altered string
54 * @see http://www.php.net/manual/function.htmlentities.php
55 * @api
56 */
57 public function render($value = NULL, $keepQuotes = FALSE, $encoding = NULL, $doubleEncode = TRUE) {
58 if ($value === NULL) {
59 $value = $this->renderChildren();
60 }
61 if (!is_string($value)) {
62 return $value;
63 }
64 if ($encoding === NULL) {
65 $encoding = $this->resolveDefaultEncoding();
66 }
67 $flags = $keepQuotes ? ENT_NOQUOTES : ENT_COMPAT;
68 return htmlentities($value, $flags, $encoding, $doubleEncode);
69 }
70
71 }