[英]how to: find the length of a typoscript defined array in fluid?
I define an array in my extensions setup.txt typoscript like so:我在我的扩展 setup.txt 排版中定义了一个数组,如下所示:
settings{
halls {
0 = Default
1 = Mississauga
2 = Halifax
3 = Niagara Falls
4 = Oakville
5 = Pickering
}}
how in my fluid template can I find the length of the halls array?如何在我的流体模板中找到 Halls 数组的长度? I want an if condition to do one thing if length is greater than 1 and another if not.如果长度大于 1,我想要一个 if 条件来做一件事,如果不是,则做另一件事。
I am using typo3 v4.5.32 with extbase 1.3.我在 extbase 1.3 中使用 Typ3 v4.5.32。
Thank you.谢谢。 I tried this but it puts out two <th>
tags instead of just one.我试过了,但它放出了两个<th>
标签,而不是一个。 The first if is meant to prove that there is more than one element, the next is meant to print out a <th>
only if it is the first element.第一个 if 是为了证明有多个元素,下一个是为了打印出<th>
仅当它是第一个元素时。
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.isFirst != itemIteration.isLast}">
<f:if condition="{itemIteration.index == 0}">
<th>
<f:translate key="tx_bpscoupons_domain_model_coupon.hall" />
</th>
</f:if>
</f:if>
</f:for>
Why?为什么?
UPDATE: here is my work around for a lack of "is array length > 1" ferature in fluid更新:这是我解决流体中缺少“数组长度> 1”功能的解决方法
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.isFirst}">
<f:then>
<f:if condition="{itemIteration.isLast}">
// if both first and last there is only one so skip
</f:if>
</f:then>
<f:else>
<f:if condition="{itemIteration.isLast}">
//if there is more than one element in halls then eventually you'll get here once so print out th tags
<th>
<f:translate key="tx_bpscoupons_domain_model_coupon.hall" />
</th>
</f:if>
</f:else>
</f:if>
</f:for>
and to print out data:并打印出数据:
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.isFirst}">
<f:then>
<f:if condition="{itemIteration.isLast}">
<f:then>
</f:then>
<f:else>
<f:if condition="{number} == {coupon.hall}">
<td>{hall}</td>
</f:if>
</f:else>
</f:if>
</f:then>
<f:else>
<f:if condition="{number} == {coupon.hall}">
<td>{hall}</td>
</f:if>
</f:else>
</f:if>
</f:for>
ugly but seems to work.丑陋但似乎工作。
UPDATE 2 : DOH!!!更新 2:DOH!!! I missed total, just what I needed but I was looking for length or count, so this code is what I want:我错过了总数,正是我需要的,但我正在寻找长度或数量,所以这段代码是我想要的:
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.total} > 1">
<f:if condition="{itemIteration.isLast}">
<th><f:translate key="tx_bpscoupons_domain_model_coupon.hall" /></th>
</f:if>
</f:if>
</f:for>
You can find a length of an array with the f:count
ViewHelper.您可以使用f:count
ViewHelper 查找数组的长度。 Just like other ViewHelper it can be used as an inline notation (You can find more here )就像其他 ViewHelper 一样,它可以用作内联符号(您可以在此处找到更多信息)
The correct code would be:正确的代码是:
<f:if condition="{settings.halls -> f.count()} > 1">
<f:then>
//your code for then
</f:then>
<f:else>
//your code for else
</f:else>
</f:if>
see update#2 above for the answer.有关答案,请参阅上面的更新#2。 to reiterate:重申:
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.total} > 1">
<f:if condition="{itemIteration.isLast}">
<th><f:translate key="tx_bpscoupons_domain_model_coupon.hall" /></th>
</f:if>
</f:if>
</f:for>
For those of you who are looking for a custom viewhelper for this:对于正在为此寻找自定义视图助手的人:
class ArrayCountViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* Get array count
*
* @param mixed $array
* @return string
*/
public function render($array) {
if (!is_array($array))
return 0;
return count($array);
}
}
<f:if condition="{f:count(subject: settings.halls)} > 1"> do something </f:if>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.