TYPO3 API  SVNRelease
AbstractConditionViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * This view helper is an abstract ViewHelper which implements an if/else condition.
00025  * @see Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode::convertArgumentValue() to find see how boolean arguments are evaluated
00026  *
00027  * = Usage =
00028  *
00029  * To create a custom Condition ViewHelper, you need to subclass this class, and
00030  * implement your own render() method. Inside there, you should call $this->renderThenChild()
00031  * if the condition evaluated to TRUE, and $this->renderElseChild() if the condition evaluated
00032  * to FALSE.
00033  *
00034  * Every Condition ViewHelper has a "then" and "else" argument, so it can be used like:
00035  * <[aConditionViewHelperName] .... then="condition true" else="condition false" />,
00036  * or as well use the "then" and "else" child nodes.
00037  *
00038  * @see Tx_Fluid_ViewHelpers_IfViewHelper for a more detailed explanation and a simple usage example.
00039  * Make sure to NOT OVERRIDE the constructor.
00040  *
00041  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00042  * @api
00043  */
00044 abstract class Tx_Fluid_Core_ViewHelper_AbstractConditionViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper implements Tx_Fluid_Core_ViewHelper_Facets_ChildNodeAccessInterface {
00045 
00046     /**
00047      * An array of Tx_Fluid_Core_Parser_SyntaxTree_AbstractNode
00048      * @var array
00049      */
00050     private $childNodes = array();
00051 
00052     /**
00053      * Setter for ChildNodes - as defined in ChildNodeAccessInterface
00054      *
00055      * @param array $childNodes Child nodes of this syntax tree node
00056      * @return void
00057      * @author Sebastian Kurfürst <sebastian@typo3.org>
00058      */
00059     public function setChildNodes(array $childNodes) {
00060         $this->childNodes = $childNodes;
00061     }
00062 
00063     /**
00064      * Initializes the "then" and "else" arguments
00065      *
00066      * @author Sebastian Kurfürst <sebastian@typo3.org>
00067      */
00068     public function __construct() {
00069         $this->registerArgument('then', 'mixed', 'Value to be returned if the condition if met.', FALSE);
00070         $this->registerArgument('else', 'mixed', 'Value to be returned if the condition if not met.', FALSE);
00071     }
00072 
00073     /**
00074      * Returns value of "then" attribute.
00075      * If then attribute is not set, iterates through child nodes and renders ThenViewHelper.
00076      * If then attribute is not set and no ThenViewHelper is found, all child nodes are rendered
00077      *
00078      * @return string rendered ThenViewHelper or contents of <f:if> if no ThenViewHelper was found
00079      * @author Sebastian Kurfürst <sebastian@typo3.org>
00080      * @author Bastian Waidelich <bastian@typo3.org>
00081      * @api
00082      */
00083     protected function renderThenChild() {
00084         if ($this->arguments->hasArgument('then')) {
00085             return $this->arguments['then'];
00086         }
00087         foreach ($this->childNodes as $childNode) {
00088             if ($childNode instanceof Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode
00089                 && $childNode->getViewHelperClassName() === 'Tx_Fluid_ViewHelpers_ThenViewHelper') {
00090                 $data = $childNode->evaluate($this->getRenderingContext());
00091                 return $data;
00092             }
00093         }
00094         return $this->renderChildren();
00095     }
00096 
00097     /**
00098      * Returns value of "else" attribute.
00099      * If else attribute is not set, iterates through child nodes and renders ElseViewHelper.
00100      * If else attribute is not set and no ElseViewHelper is found, an empty string will be returned.
00101      *
00102      * @return string rendered ElseViewHelper or an empty string if no ThenViewHelper was found
00103      * @author Sebastian Kurfürst <sebastian@typo3.org>
00104      * @author Bastian Waidelich <bastian@typo3.org>
00105      * @api
00106      */
00107     protected function renderElseChild() {
00108         foreach ($this->childNodes as $childNode) {
00109             if ($childNode instanceof Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode
00110                 && $childNode->getViewHelperClassName() === 'Tx_Fluid_ViewHelpers_ElseViewHelper') {
00111                 return $childNode->evaluate($this->getRenderingContext());
00112             }
00113         }
00114         if ($this->arguments->hasArgument('else')) {
00115             return $this->arguments['else'];
00116         }
00117         return '';
00118     }
00119 }
00120 
00121 ?>