Skip to content

Instantly share code, notes, and snippets.

@josefglatz
Last active September 11, 2020 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefglatz/a7246c8c0487f66381f4a65a4d964b7c to your computer and use it in GitHub Desktop.
Save josefglatz/a7246c8c0487f66381f4a65a4d964b7c to your computer and use it in GitHub Desktop.
ViewHelper for your sitepackage extension (e.g. ext:theme/Classes/ViewHelpers/Format/RemoveEmptyParagraphViewHelper.php)
<?php
declare(strict_types = 1);
namespace JosefGlatz\Theme\ViewHelpers\Format;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
/**
* Remove empty P tags from $content by using preg_replace
*/
class RemoveEmptyParagraphViewHelper extends AbstractViewHelper
{
use CompileWithContentArgumentAndRenderStatic;
/**
* @var bool
*/
protected $escapeOutput = false;
/**
* @return void
*/
public function initializeArguments()
{
$this->registerArgument('content', 'string', 'String to be relieved of empty paragraphs');
}
/**
* Return $content without empty paragraphs
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$content = $renderChildrenClosure();
$content = preg_replace('~\\s?<p>(?:\\s|&nbsp;)+</p>\\s?~', '', $content);
return $content;
}
}
<!-- The ViewHeler can be used in a Fluid template to cleanup the gibberish content of editors which mean that you make spaces with empty paragraphs -->
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
xmlns:theme="http://typo3.org/ns/JosefGlatz/Theme/ViewHelpers" data-namespace-typo3-fluid="true">
<f:spaceless>
<div class="CE CE_TEXTCENTERED {data.space_before_class} {data.space_after_class}" id="c{data.uid}">
<f:if condition="{data._LOCALIZED_UID}">
<a id="c{data._LOCALIZED_UID}"></a>
</f:if>
<div class="container-wrapper">
<div class="row">
<div class="col-12 text-center">
{data.bodytext -> f:format.html() -> theme:format.removeEmptyParagraph()}
</div>
</div>
</div>
</div>
</f:spaceless>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment