22725f2d126fd3cc1f92702487203850527ce33e
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / Nl2brViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
18 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
19
20 /**
21 * Wrapper for PHPs nl2br function.
22 * @see http://www.php.net/manual/en/function.nl2br.php
23 *
24 * = Examples =
25 *
26 * <code title="Example">
27 * <f:format.nl2br>{text_with_linebreaks}</f:format.nl2br>
28 * </code>
29 * <output>
30 * text with line breaks replaced by <br />
31 * </output>
32 *
33 * <code title="Inline notation">
34 * {text_with_linebreaks -> f:format.nl2br()}
35 * </code>
36 * <output>
37 * text with line breaks replaced by <br />
38 * </output>
39 *
40 * @api
41 */
42 class Nl2brViewHelper extends AbstractViewHelper
43 {
44 /**
45 * @var bool
46 */
47 protected $escapeOutput = false;
48
49 /**
50 * Replaces newline characters by HTML line breaks.
51 *
52 * @param string $value string to format
53 * @return string the altered string.
54 * @api
55 */
56 public function render($value = null)
57 {
58 return static::renderStatic(
59 array(
60 'value' => $value
61 ),
62 $this->buildRenderChildrenClosure(),
63 $this->renderingContext
64 );
65 }
66
67 /**
68 * Applies nl2br() on the specified value.
69 *
70 * @param array $arguments
71 * @param \Closure $renderChildrenClosure
72 * @param \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
73 * @return string
74 */
75 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
76 {
77 $value = $arguments['value'];
78 if ($value === null) {
79 $value = $renderChildrenClosure();
80 }
81
82 return nl2br($value);
83 }
84 }