答案 0 :(得分:0)
因为ObjectStorages没有数字索引,所以我创建了一个自己的viewhelper(在DCE扩展名中),使ObjectStorages数组成为数字。它还会返回给定的索引。
示例:{spareparts.0.categories -> dce:arrayGetIndex(index:1)}
这将返回第二个类别。默认值为index:0,返回第一个项目。
这种方法比使用f:for
iterator.isFirst
循环更好。如果查询结果中有许多类别,则可能会出现性能问题。
此viewhelper的代码非常简单:
<?php
namespace ArminVieweg\Dce\ViewHelpers;
/* | This extension is made for TYPO3 CMS and is licensed
* | under GNU General Public License.
* |
* | (c) 2012-2017 Armin Ruediger Vieweg <armin@v.ieweg.de>
*/
/**
* Returns the given index of an array.
*
* @package ArminVieweg\Dce
*/
class ArrayGetIndexViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
/**
* Returns the value of the given index in the given array. To make sure the indexes are numeric the array will be
* converted. Named array keys will be overwritten by ascending index numbers (starting with 0).
*
* @param array $subject The array to get the value of
* @param int|string $index Index of array. May be int or string. Default is zero (0).
* @return mixed The value of the given array index
*/
public function render(array $subject = null, $index = 0)
{
if ($subject === null) {
$subject = $this->renderChildren();
}
$subject = array_values($subject);
return $subject[$index];
}
}
希望它有所帮助。