ew_base/Classes/ViewHelpers/ReplaceViewHelper.php
2024-12-26 21:44:19 +01:00

62 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
/*
* 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\ViewHelper\AbstractViewHelper;
/**
* 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
{
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 function render(): string
{
$content = $this->arguments['value'];
if ($content === null) {
$content = $this->renderChildren();
}
return str_replace($this->arguments['search'], $this->arguments['replace'], $content);
}
}