Created
March 29, 2019 11:15
-
-
Save pixelbrackets/ac49c3c03ac5f8a72143a37b93c24213 to your computer and use it in GitHub Desktop.
TYPO3 Fluid ViewHelper to determine whether a variable is non empty
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
<?php | |
namespace Pixelbrackets\AcmeExtension\ViewHelpers; | |
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper; | |
/** | |
* Determine whether a variable is non empty | |
* | |
* Non Empty Wording: https://english.stackexchange.com/a/102789 | |
* Values considered as empty: https://www.php.net/manual/en/function.empty.php | |
* | |
* = Examples = | |
* | |
* <code title="Check value"> | |
* <w:ifNonEmpty value="{someValue}"> | |
* <f:then> | |
* condition is met | |
* </f:then> | |
* <f:else> | |
* condition is not met | |
* </f:else> | |
* </w:ifNonEmpty> | |
* </code> | |
* <output> | |
* Everything inside the "then" tag is displayed if the condition evaluates to TRUE. | |
* Otherwise, everything inside the "else"-tag is displayed. | |
* </output> | |
* | |
* <code title="Inline notation"> | |
* {w:ifNonEmpty(value: '{someValue}', then: 'condition is met', else: 'condition is not met')} | |
* </code> | |
* <output> | |
* Everything inside the "then" tag is displayed if the condition evaluates to TRUE. | |
* Otherwise, everything inside the "else"-tag is displayed. | |
* </output> | |
* | |
* @category ViewHelper | |
* @author Dan Untenzu <untenzu@webit.de>, webit! Gesellschaft für neue Medien mbH | |
* @license The GNU General Public License | |
* @link http://www.gnu.org/copyleft/gpl.html | |
*/ | |
class IfNonEmptyViewHelper extends AbstractConditionViewHelper | |
{ | |
/** | |
* Initialize arguments. | |
* | |
* @return void | |
* @api | |
*/ | |
public function initializeArguments() | |
{ | |
parent::initializeArguments(); | |
$this->registerArgument( | |
'value', | |
'mixed', | |
'Value to determine whether it is non empty', | |
true, | |
'' | |
); | |
} | |
/** | |
* Determine whether a variable is non empty | |
* | |
* @return string The rendered string | |
*/ | |
public function render() | |
{ | |
if (static::evaluateCondition($this->arguments)) { | |
return $this->renderThenChild(); | |
} | |
return $this->renderElseChild(); | |
} | |
/** | |
* Condition to evaluate - Returns true if the given value is not empty | |
* | |
* @param array|NULL $arguments | |
* @return boolean | |
* @api | |
*/ | |
protected static function evaluateCondition($arguments = null) | |
{ | |
$result = !empty($arguments['value']); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment