[TYPO3-mvc] Re: AppendArrayViewHelper

Nils Blattner nb at cabag.ch
Fri Feb 19 10:14:07 CET 2010


I had a few points where I had to create different links from the same 
template which essentially had the same arguments except a pager variable.
For that I made this ViewHelper.

Usage:
For the full option list, check the @param's.

<namespace:appendArray array="{someArray}" key="property" 
value="{someProperty}" alias="aliasArray">
	{aliasArray.property}
</namespace:appendArray>

Or inline:

{appendArray(array: someArray, value: someOtherArray)}
would return the merged array.

<?php

/*                                                                        *
  * This script is part of the TYPO3 project - inspiring people to share!  *
  *                                                                        *
  * TYPO3 is free software; you can redistribute it and/or modify it under *
  * the terms of the GNU General Public License version 2 as published by  *
  * the Free Software Foundation.                                          *
  *                                                                        *
  * This script is distributed in the hope that it will be useful, but     *
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
  * Public License for more details.                                       *
  * 
    */

/**
  * This ViewHelper appends a value to an array.
  *
  * @author Nils Blattner <nb at cabag.ch>
  * @version $Id:$
  * @license http://opensource.org/licenses/gpl-license.php GNU Public 
License, version 2
  * @scope prototype
  */
class Tx_CabagSteps_ViewHelpers_AppendArrayViewHelper extends 
Tx_Fluid_Core_ViewHelper_AbstractViewHelper {

	/**
	 * Appends the value to the array with key if given.
	 *
	 * @param array $array Array to be appended to
	 * @param string $key Key if to be added as an associative item, leave 
blank to be added with a number key
	 * @param mixed $value If array it will add all items with the 
respective keys
	 * @param boolean $forceNumeric If set to true it will ignore 
associative keys
	 * @param boolean $forceAsValue If set to true it will add the array as 
a subvalue
	 * @param string $alias If this is set, the array will be set as an 
alias instead of returned.
	 * @return array The resulting array
	 * @author Nils Blattner <nb at cabag.ch>
	 */
	public function render(array $array = NULL, $key = '', $value = '', 
$forceNumeric = false, $forceAsValue = false, $alias = '') {
		if ($array == null) {
			$array = array();
		}
		if (!$forceAsValue && is_array($value)) {
			foreach ($value as $k => $v) {
				if (!$forceNumeric) {
					$array[$k] = $v;
				} else {
					$array[] = $v;
				}
			}
		} else {
			if (!$forceNumeric && is_string($key) && $key != '') {
				$array[$key] = $value;
			} else {
				$array[] = $value;
			}
		}
		
		if (!empty($alias) && is_string($alias)) {
			$this->templateVariableContainer->add($alias, $array);
			
			$output = $this->renderChildren();
			
			$this->templateVariableContainer->remove($alias);
			
			return $output;
		}
		
		return $array;
	}
}


?>


More information about the TYPO3-project-typo3v4mvc mailing list