TYPO3 API  SVNRelease
CountViewHelper.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 ViewHelper counts elements of the specified array or countable object.
00025  *
00026  * = Examples =
00027  *
00028  * <code title="Count array elements">
00029  * <f:count subject="{0:1, 1:2, 2:3, 3:4}" />
00030  * </code>
00031  * <output>
00032  * 4
00033  * </output>
00034  *
00035  * <code title="inline notation">
00036  * {objects -> f:count()}
00037  * </code>
00038  * <output>
00039  * 10 (depending on the number of items in {objects})
00040  * </output>
00041  *
00042  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00043  * @api
00044  */
00045 class Tx_Fluid_ViewHelpers_CountViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00046 
00047     /**
00048      * Counts the items of a given property.
00049      *
00050      * @param array $subject The array or ObjectStorage to iterated over
00051      * @return integer The number of elements
00052      * @author Jochen Rau <jochen.rau@typoplanet.de>
00053      * @author Bastian Waidelich <bastian@typo3.org>
00054      * @api
00055      */
00056     public function render($subject = NULL) {
00057         if ($subject === NULL) {
00058             $subject = $this->renderChildren();
00059         }
00060         if (is_object($subject) && !$subject instanceof Countable) {
00061             throw new Tx_Fluid_Core_ViewHelper_Exception('CountViewHelper only supports arrays and objects implementing Countable interface. Given: "' . get_class($subject) . '"', 1279808078);
00062         }
00063         return count($subject);
00064     }
00065 }
00066 
00067 ?>