Search code examples
typo3typoscriptfluidtx-news

Number of alternating template parts in tx_news


Is there any possibilities tu use something like the alternating template parts in tx_news extension? In standard tt_news template I used to use <!-- ###NEWS_1###--> <!-- ###NEWS_2###--> <!-- ###NEWS_3###--> etc. In tx_news everything is in Fluid and I don't see anything similar to alternating template parts in manual.


Solution

  • Ok, again as answer:

    You can just do this in fluid, there is no need for a specific feature. The news are rendered in a for-loop, which provides a variable with the current index, see the documentation.

    Use the loop index modulo your number of different templates to render news differently in an alternating way. The iteration-variable provides some more subproperties that you can use for controlling the output. To see them all, use <f:debug>{iterator}</f:debug>.

    For example, in the list-view of EXT:news you can do this to get three alternating templates, each one represented by a partial. Only the relevant inner loop is shown:

    <f:for each="{news}" as="newsItem" iteration="iterator">
        <f:if condition="{iterator.index} % 3 == 0">
            <f:render partial="List/Item_Layout1" arguments="{newsItem: newsItem,settings:settings,iterator:iterator}" />
        </f:if>
    
        <f:if condition="{iterator.index} % 3 == 1">
            <f:render partial="List/Item_Layout2" arguments="{newsItem: newsItem,settings:settings,iterator:iterator}" />
        </f:if>
    
        <f:if condition="{iterator.index} % 3 == 2">
            <f:render partial="List/Item_Layout3" arguments="{newsItem: newsItem,settings:settings,iterator:iterator}" />
        </f:if>
    </f:for>
    

    If you have EXT:vhs installed with namespace shortcut v (very good extension!), this can be done a bit more elegant:

    <f:for each="{news}" as="newsItem" iteration="iterator">
        <v:switch value="{iterator.index} % 3">
            <v:case value="0" break="true">
                <f:render partial="List/Item_Layout1" arguments="{newsItem: newsItem,settings:settings,iterator:iterator}" />
            </v:case>
    
            <v:case value="1" break="true">
                <f:render partial="List/Item_Layout2" arguments="{newsItem: newsItem,settings:settings,iterator:iterator}" />
            </v:case>
    
            <v:case value="2" break="true">
                <f:render partial="List/Item_Layout3" arguments="{newsItem: newsItem,settings:settings,iterator:iterator}" />
            </v:case>
        </v:switch>