typo3fluid

Create a Fluid loop for a chosen number of Times Item.number_stars


I need to run a fluid loop with chosen number of times, This number of stars is an Integer and not an array, integer from 1 - 5. {Records.numberStars} type INT.

<f:for each="{record.data.stars}" as="stars" iteration="StarsIterator">
    <f:if condition="{stars} < {StarsIterator}">
        <f:render partial="Icon" "/>
    </f:if>
</f:for>

Is there any way to make a loop limited to an integer variable ? Fluid template TYPO3 v12.4


Solution

  • f:for expects each to be an array, not an integer. So it's not possible to use that if I get you right and {record.data.stars} is an integer and not an array.

    There is no beautiful solution out of the box for your problem. But here are some possible solutions:

    Use the PHP controller or a DataProcessor to deliver an array

    $stars = [
        0 => 1,
        1 => 1,
        2 => 1,
        3 => 1
    ];
    
    <f:for each="{record.data.stars}" as="stars">
        <f:render partial="Icon" "/>
    </f:for>
    

    But if you get the integer value right from a database record, that might not work or you need another custom DataProcessor.

    Use f:switch ViewHelper

    This solution is rather ugly.

    <f:switch expression="{record.data.stars}">
        <f:case value="5">
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
        </f:case>
        <f:case value="4">
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
        </f:case>
        <f:case value="3">
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
        </f:case>
        <f:case value="2">
            <f:render partial="Icon" "/>
            <f:render partial="Icon" "/>
        </f:case>
        <f:case value="1">
            <f:render partial="Icon" "/>
        </f:case>
    </f:switch>
    

    Use v:iterator.loop from EXT:vhs

    The TYPO3 extension vhs provides a lot of additional ViewHelpers. But it also adds a dependency to your project and makes you dependent on updates by the author of the extension.

    But v:iterator.loop does exactly what you want:

    <v:iterator.loop count="{record.data.stars}" iteration="star">
        <f:render partial="Icon" "/>
    </v:iterator.loop>
    

    Develop a custom ViewHelper

    Of course it's also possible to extract the functionality of v:iterator.loop and put it in your own custom ViewHelper.