[英]Is there a way to wrap X items in TYPO3 fluid?
Using the extension DCE elements, I created a fluid template generating a li item for every item created. 使用扩展DCE元素,我创建了一个流体模板,为创建的每个项目生成一个li项目。 Now I would like to have every first to second item a wrapper.
现在,我想将第一到第二个项目都包装。 So that:
以便:
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
<li>item 5</li>
Becomes: 变为:
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
<ul>
<li>item 3</li>
<li>item 4</li>
</ul>
<ul>
<li>item 5</li>
</ul>
Is this possible in a fluid template? 流体模板中可能吗? Perhaps in combination with Typoscript?
也许结合Typoscript?
This is what I ended up with using {cycle}
to iterate trough every 2 items, and then closing and opening the tag. 这就是我最终使用
{cycle}
来每隔2个项目迭代一次,然后关闭并打开标签的结果。 As well as making sure the last item does not end with an opening tag: 确保最后一项不以开始标签结尾:
<ul>
<f:for each="{field.item}" as="item" iteration="iterator">
<f:if condition="{iterator.cycle} % 2">
<f:then>
<li>item {iterator.cycle}</li>
</f:then>
<f:else>
<li>item {iterator.cycle}</li>
<f:if condition="{iterator.isLast} != 1">
<f:then>
</ul><ul>
</f:then>
</f:if>
</f:else>
</f:if>
</f:for>
</ul>
I understand that with the power of VHS v:iterator.chunk
would probably solve this with a little less code, but I could not get it to work how I wanted to. 我知道,借助VHS
v:iterator.chunk
的强大功能,可以用更少的代码来解决此问题,但是我无法使其按预期工作。 If anyone would be willing to provide a working example to compare the 2 options I would be grateful. 如果有人愿意提供一个可行的例子来比较这两种选择,我将不胜感激。
Update: 21.12.2017 更新:21.12.2017
So revisited the chunk option, and the same code is achieved with the following snippet using VHS iterator.chunk: 因此,重新讨论了块选项,并使用VHS iterator.chunk使用以下代码段实现了相同的代码:
<f:for each="{field.item -> v:iterator.chunk(count: 2)}" as="col" iteration="cycle">
<ul>
<f:for each="{col}" as="item">
<li>
test
</li>
</f:for>
</ul>
</f:for>
Perhaps this helps someone out there! 也许这可以帮助某个人!
If you are using the VHS view helpers collection extension, then you can also use the Iterator\\Chunked
view helper: 如果使用的是VHS视图帮助程序集合扩展,则还可以使用
Iterator\\Chunked
视图帮助程序:
<v:iterator.chunk count="4" subject="{menu}" as="entries">
<f:for each="{entries}" as="row">
<f:for each="{row}" as="page">
[...]
</f:for>
</f:for>
</v:iterator>
Sure, you need the for
viewhelper in addition to its counters/iterators. 当然,除了其计数器/迭代器外,您还需要
for
viewhelper。 The you can use the if
viewhelper to check for your condition. 您可以使用
if
viewhelper来检查您的状况。 The API has some examples: http://api.typo3.org/typo3cms/current/html/class_t_y_p_o3_1_1_c_m_s_1_1_fluid_1_1_view_helpers_1_1_for_view_helper.html#details API包含一些示例: http : //api.typo3.org/typo3cms/current/html/class_t_y_p_o3_1_1_c_m_s_1_1_fluid_1_1_view_helpers_1_1_for_view_helper.html#details
https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/For.html https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/For.html
This is possible with regular Fluid, using the iterator of the for
view helper. 使用
for
view助手的迭代器,使用常规Fluid可以做到这一点。
<f:for each="{items}" as="item" iteration="itemIterator">
<f:if condition="{itemIterator.isOdd}">
<ul>
</f:if>
<li>{item.name}</li>
<f:if condition="{itemIterator.isOdd}">
<f:then>
<f:if condition="{itemIterator.isLast}">
</ul>
</f:if>
</f:then>
<f:else>
</ul>
</f:else>
</f:if>
</f:for>
The tricky part is the closing </ul>
which is covered with the Fluid above. 棘手的部分是关闭
</ul>
,上面有Fluid所覆盖。
I have one more solution, As for me - more native 我还有一个解决方案,至于我-更本地化
<f:for each="{items}" as="item" iteration="itemIterator">
<f:cycle values="{0:'left', 1:'right', 3:'center'}" as="order">
<f:switch expression="{order}">
<f:case value="left">
<div class="left">Conetnt</div>
</f:case>
<f:case value="right">
<div class="right">Conetnt</div>
</f:case>
<f:case value="center">
<div class="center">Conetnt</div>
</f:case>
</f:switch>
</f:cycle>
</f:for>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.