You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3 b13/container扩展:子元素渲染用iterator.index并保留assets

解决TYPO3 b13/container传递子元素到partial时丢失assets并生成编号ID的问题

问题原因

你遇到的assets丢失问题,本质是因为直接传递child到partial后,若没有直接使用child.renderedContent,而是尝试重新渲染子元素内容,会绕过TYPO3的asset收集机制。child.renderedContent是已经经过完整渲染流程的结果,其中已经包含了子元素依赖的JS/CSS资源引用,重新渲染会导致这些资源无法被正确收集和输出。

解决方案

核心原则是直接使用child.renderedContent输出内容,同时将iterator.index传递到partial用于生成编号ID,以下是两种常见实现方式:

方式一:仅传递渲染好的内容和索引(最简洁)

修改主模板的循环代码,将child.renderedContent和iterator.index作为参数传入partial:

<f:for each="{children_200}" as="child" iteration="iterator">
    <f:render partial="ContainerItem" arguments="{renderedContent: child.renderedContent, itemIndex: iterator.index}" />
</f:for>

在ContainerItem.html partial中,使用索引生成ID并输出内容:

<div id="container-item-{itemIndex}">
    {renderedContent -> f:format.raw()}
</div>

方式二:传递完整child对象+索引(需用到child其他数据时)

如果partial中需要用到子元素的其他数据(比如child.data中的字段),可以传递完整的child和索引:

<f:for each="{children_200}" as="child" iteration="iterator">
    <f:render partial="ContainerItem" arguments="{child: child, itemIndex: iterator.index}" />
</f:for>

在partial中,依然直接使用child.renderedContent输出内容,同时用索引生成ID:

<div id="container-item-{itemIndex}">
    {child.renderedContent -> f:format.raw()}
    <!-- 可额外使用child的其他数据,比如:{child.data.header} -->
</div>

关键注意事项

  • 绝对不要在partial中重新渲染子元素内容(比如用<f:cObject data="{child.data}" typoscriptObjectPath="lib.contentElement" />这类方式),必须直接使用child.renderedContent才能保留assets。
  • iterator.index是从1开始的序列,若需要从0开始,可使用iterator.cycle。

内容的提问来源于stack exchange,提问作者webman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 12:52:56