Skip to content

Commit

Permalink
[FEATURE] Format new line to paragraph viewhelper
Browse files Browse the repository at this point in the history
Most of the time the format nl2br viewhelper produces
unwanted and unnessesary line breaks where a paragraph
is acturally the wanted result. The new viewhelper converts
linebreaks to actual paragraphs and cleans out empty
segments. In contrary to the form html viewhelper anything
else wil be left untouched.

Releases: master
  • Loading branch information
benjaminkott committed May 4, 2019
1 parent a17891e commit 7e4443e
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Classes/ViewHelpers/Format/Nl2pViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types = 1);

/*
* This file is part of the package t3g/blog.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace T3G\AgencyPack\Blog\ViewHelpers\Format;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;

class Nl2pViewHelper extends AbstractViewHelper
{
use CompileWithContentArgumentAndRenderStatic;

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

/**
* @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
*/
public function initializeArguments()
{
$this->registerArgument('value', 'string', 'string to format');
}

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
* @return string
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$data = explode('<br>', nl2br($renderChildrenClosure(), false));
$data = array_filter($data, function ($value) {
return !empty(trim($value));
});
return '<p>' . implode('</p><p>', $data) . '</p>';
}
}

0 comments on commit 7e4443e

Please sign in to comment.