【问题标题】:Typo3 nested arrays in FluidFluid 中的 Typo3 嵌套数组
【发布时间】:2016-08-31 12:28:27
【问题描述】:

我正在将一个嵌套数组传递给我的 Fluid-Template,现在想要遍历它们。

唉,第二个 for-each 只显示超数组的键?

数组(伪代码):

[2016]
   [0]
      [title]->test
      [content]->test
   [1]
      [title]->test
      [content]->test

还有我模板中的代码:

<f:for each="{myArray}" as="topItem" iteration="it1" key="key">

    <h4>{key}</h4>
    <f:for each="{topItem}" as="subItem" iteration="it2">
        {subItem.title}<br />
        {subItem.content}
    </f:for>
</f:for>

我做错了什么?

【问题讨论】:

  • 当然看起来没有错 - 你能告诉我们&lt;f:debug&gt;{myArray}&lt;/f:debug&gt;的输出(截图)吗?
  • 我设法让它与 一起工作 - 但感谢您的回复!
  • 请接受/关闭问题 - 并可能添加您用来使其工作的最终代码;)
  • 不过,这里发布的流体应该确实有效。

标签: typo3 fluid


【解决方案1】:

作为答案和示例的 cmets 的摘要 f:forf:groupedFor view-helpers。 f:for 基本上适用于任何类型的数组,并允许迭代嵌套数据的每一层 - f:group 非常适合将平面数据结构转换为嵌套数据集。

f:for

输入数组是二维的,如下所示

$this->view->assign('nestedItems', [
    '2016' => [
        ['title' => '1st Title', 'content' => '1st Content'],
        ['title' => '2nd Title', 'content' => '2nd Content'],
    ],
    '2015' => [
        ['title' => '3rd Title', 'content' => '3rd Content'],
        ['title' => '4th Title', 'content' => '4th Content'],
    ],
]);

用于迭代嵌套数据集的 Fluid 模板如下所示

<f:for each="{nestedItems}" as="items" key="currentYear">
    <h2>{currentYear}</h2>
    <f:for each="{items}" as="item">
      <h3>{item.title}</h3>
      <p>{item.content}</p>
    </f:for>
</f:for>

f:groupedFor

平面输入数组如下所示(年份现在是每个数组元素的一部分)

$this->view->assign('flatItems', [
    ['year' => 2016, 'title' => '1st Title', 'content' => '1st Content'],
    ['year' => 2016, 'title' => '2nd Title', 'content' => '2nd Content'],
    ['year' => 2015, 'title' => '3rd Title', 'content' => '3rd Content'],
    ['year' => 2015, 'title' => '4th Title', 'content' => '4th Content'],
]);

用于迭代嵌套数据集的 Fluid 模板如下所示

<f:groupedFor each="{flatItems}" as="items" groupBy="year" groupKey="currentYear">
  <h2>{currentYear}</h2>
  <f:for each="{items}" as="item">
    <h3>{item.title}</h3>
    <p>{item.content}</p>
  </f:for>
</f:groupedFor>

两种方案的输出

两种情况下的输出是一样的

<h2>2016</h2>

    <h3>1st Title</h3>
    <p>1st Content</p>

    <h3>2nd Title</h3>
    <p>2nd Content</p>

<h2>2015</h2>

    <h3>3rd Title</h3>
    <p>3rd Content</p>

    <h3>4th Title</h3>
    <p>4th Content</p>

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 2014-01-13
    • 2018-09-29
    • 2014-09-06
    • 2017-01-10
    • 2014-02-22
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多