在前端按年份分组新闻(TYPO3,新闻系统)

use*_*531 0 typo3 tx-news typo3-9.x

我正在使用 TYPO3 9,新闻插件(新闻系统)。

我正在寻找一种按年份对新闻文章进行分组的方法,如下所示:

2019
--------
article 1
article 2
article 3
--------
2018
--------
article 1
article 2
...
Run Code Online (Sandbox Code Playgroud)

我找不到一个简单的解决方案,很难相信实现这一点的唯一方法是编辑 TYPO3 的源代码......

任何人都可以帮忙吗?

- - - - - - - - - - - - - 编辑

Bernd Wilke 建议的固定和工作代码:

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
      xmlns:n="http://typo3.org/ns/GeorgRinger/News/ViewHelpers"
      data-namespace-typo3-fluid="true">
<f:layout name="General" />
<!--
    =====================
        Templates/News/List.html
-->

<f:section name="content">
    <!--TYPO3SEARCH_end-->
    <f:if condition="{news}">
        <f:then>
            <f:variable name="oldYear">2010</f:variable>
            <f:for each="{news}" as="newsItem" iteration="iterator">
                <f:variable name="currentYear"><f:format.date format="%Y">{newsItem.datetime}</f:format.date></f:variable>
                <f:if condition="{oldYear} < {currentYear}">
                    <hr />
                    <h3>{currentYear}</h3>
                    <hr />
                </f:if>
                <f:render partial="List/Item" arguments="{newsItem: newsItem,settings:settings,iterator:iterator}" />
            </f:for>
        </f:then>
        <f:else>
            <div class="alert ">
                <f:translate key="list_nonewsfound" />
            </div>
        </f:else>
    </f:if>
    <!--TYPO3SEARCH_begin-->
</f:section>
</html>
Run Code Online (Sandbox Code Playgroud)

但是,我将这个问题交给了 Georg Ringer,因为他的解决方案很快就奏效了。

Geo*_*ger 5

当然,这可以通过使用 ViewHelper 来实现f:groupedFor,请参阅官方文档

新闻扩展文档中也有一个例子。所以这个例子应该像那样工作

<f:groupedFor each="{news}" as="groupedNews" groupBy="yearOfDatetime" groupKey="year">
        <div style="border:1px solid blue;padding:10px;margin:10px;">
                <h1>{year}</h1>
                <f:for each="{groupedNews}" as="newsItem">
                        <div style="border:1px solid pink;padding:5px;margin:5px;">
                                {newsItem.title}
                        </div>
                </f:for>
        </div>
</f:groupedFor>
Run Code Online (Sandbox Code Playgroud)

但是我也想警告以下几点

关注性能!

为了能够对记录进行分组,fluid 将加载每条记录本身,然后将它们分组。如果您打算将许多记录分组只是为了获得诸如计数之类的东西,也许最好直接触发查询并且不要为此使用流体。

但是,如果结果在可缓存的页面上,则问题仅与第一次命中有关。