ew_base/Classes/ViewHelpers/ReplaceViewHelper.php
2024-05-25 16:24:24 +02:00

67 lines
2.1 KiB
PHP
Executable File

<?php
/*
* This file is developed by evoWeb.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
namespace Evoweb\EwBase\ViewHelpers;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
/**
* Variable assigning ViewHelper
*
* Assigns one template variable which will exist also
* after the ViewHelper is done rendering, i.e. adds
* template variables.
*
* If you require a variable assignment which does not
* exist in the template after a piece of Fluid code
* is rendered, consider using ``f:alias`` ViewHelper instead.
*
* Usages:
*
* ::
*
* {f:variable(name: 'myvariable', value: 'some value')}
* <f:variable name="myvariable">some value</f:variable>
* {oldvariable -> f:format.htmlspecialchars() -> f:variable(name: 'newvariable')}
* <f:variable name="myvariable"><f:format.htmlspecialchars>{oldvariable}</f:format.htmlspecialchars></f:variable>
*
* @see \TYPO3Fluid\Fluid\ViewHelpers\IfViewHelper
* @api
*/
class ReplaceViewHelper extends AbstractViewHelper
{
use CompileWithContentArgumentAndRenderStatic;
public function initializeArguments(): void
{
$this->registerArgument('value', 'string', 'String to replace in');
$this->registerArgument('search', 'string', 'Search string');
$this->registerArgument('replace', 'string', 'Replace value');
}
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
): string {
$content = $arguments['value'];
if ($content === null) {
$content = $renderChildrenClosure();
}
return str_replace($arguments['search'], $arguments['replace'], $content);
}
}