Typo3 Fluid:如果元素是第一个,则将 Class 添加到元素

Typo3 Fluid: Add Class to Element if it is First

我正在使用 Fluid 将 Contentelments 提供给扩展:

<f:for each="{field.children}" as="myObj" iteration="iterator">
  {myObj.render -> f:format.raw()}
</f:for>

工作正常。

元素看起来像这样:

<div class="childElement">
  <h1>I'm a Caption!</h1>
  <p>I'm some text.....</p>
</div>

然后元素以特定方式显示。

现在我希望第一个元素一个额外的class叫做“第一个”。第一个元素将如下所示:

<div class="childElement first">
  <h1>I'm a Caption!</h1>
  <p>I'm some text.....</p>
</div>

我试过 使用迭代器解决这个问题并简单地检查迭代器是否 "isfirst"。像这样:

<f:for each="{field.children}" as="myObj" iteration="iteration">
 <f:if condition="iteration.isFirst">
    <f:then>
     ...

但是我仍然需要插入 class 并且无论如何感觉必须有更好的方法来解决它。

有人可以指出我正确的方向吗?


编辑(解决方案):

在我执行 "<div class="childElement">" 后,接受的答案对我有用 - 在扩展中而不是在 Child 中包装。 我的解决方案现在看起来像这样:

<f:for each="{field.children}" as="myObj" iteration="iterator">
  <div class="childElement {f:if(condition:iterator.isFirst, then: ' first')}">
     {childDce.render -> f:format.raw()}
   </div>
</f:for>

您可以简单地使用 f:if viewhelper 的内联版本来避免在 <f:then> -> <f:else> 结构中重复自己:

<f:for each="{field.children}" as="myObj" iteration="iter">
    <div class="childElement{f:if(condition:iter.isFirst, then: ' first')}">

因为你没有 else 案例,你可以放弃它。 根据您的流体版本,您可能需要用更多花括号包裹您的条件表达式,如下所示:

<f:for each="{field.children}" as="myObj" iteration="iter">
    <div class="childElement{f:if(condition: '{iter.isFirst}', then: ' first')}">

但这不是必需的(尽管它有效)。