-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Format new line to paragraph viewhelper
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
1 parent
a17891e
commit 7e4443e
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'; | ||
} | ||
} |