Library · Code examples

Chunked iteration of lists

Wrap or group elements with less code than ever.

Description TYPO3 fluid VHS: Group elements with wrap without Modulo
Author githubrsys
Creation date 2016-02-09T15:32:42.000Z
Extensions
  1. FluidTYPO3.Vhs
Tags
  1. Content
  2. ViewHelpers
Files
  1. readme.md

Summary

Imagine you have a list of elements. E.g. some DIV's (or something else) and you want to surround or wrap blocks or chunks of 3 with an <li> element.

Expected output Code

<li>
<div>foo</div>
<div>foo</div>
<div>foo</div>
</li>
<li>
<div>foo</div>
<div>foo</div>
<div>foo</div>
</li>
<li>
<div>foo</div>
<div>foo</div>
<div>foo</div>
</li>

Solution with VHS

<f:for each="{foo -> v:iterator.chunk(count: 3)}" as="bar" iteration="cycle">
    <li>
        <f:for each="{bar}" as="user">
            <f:render section="yourTarget" arguments="{_all}" />
        </f:for>
    </li>
</f:for>

Solution with pure fluid

<f:for each="{foo}" as="bar" iteration="cycle">
        <f:if condition="{cycle.isFirst}">
            <li>
        </f:if>
        <f:if condition="{cycle.cycle} % 3">
             <f:then>
             <f:render section="yourTarget" arguments="{_all}" />
             </f:then>
             <f:else>
             <f:render section="yourTarget" arguments="{_all}" />
                 <f:if condition="{cycle.isLast}">
                     <f:then></f:then>
                     <f:else>
                         </li>
                         <li>
                     </f:else>
                 </f:if>
             </f:else>
        </f:if>
        <f:if condition="{cycle.isLast}">
            </li>
        </f:if>
</f:for>

What you need

Conclusion

Feel the force, Luke.

Thanks

@cedricziel

view raw readme.md hosted with ❤ by GitHub