如何在Typo3中重写Fluid模板时按需渲染原扩展模板
在TYPO3中自定义News扩展模板时,条件不满足时渲染原模板的方法
问题场景
我需要在TYPO3中用自定义Fluid模板重写News扩展的列表模板,目的是新增自定义模板布局(当settings.templateLayout == 2时启用自定义布局),同时在条件不满足时保留News扩展的默认模板渲染逻辑。当前自定义模板结构如下,需要补充else分支的原模板引入代码:
<!-- my_site_package/Resources/Private/Extensions/News/Templates/News/List.html --> <f:if condition="{settings.templateLayout} == 2"> <f:then> My specific list view for news entries <ul> <li> ... </li> </ul> </f:then> <f:else> <!-- Include the original template here, e.g. EXT:news/Resources/Private/Templates/Styles/Twb5/Templates/News/List.html --> </f:else> </f:if>
解决方案
在else分支中,使用Fluid的<f:render>标签直接引入原扩展的模板文件,同时通过arguments="{_all}"传递当前所有变量,确保原模板能正常获取所需的新闻数据、配置参数等。
修改后的完整代码如下:
<!-- my_site_package/Resources/Private/Extensions/News/Templates/News/List.html --> <f:if condition="{settings.templateLayout} == 2"> <f:then> My specific list view for news entries <ul> <li> ... </li> </ul> </f:then> <f:else> <!-- 引入News扩展的原列表模板 --> <f:render template="EXT:news/Resources/Private/Templates/Styles/Twb5/Templates/News/List.html" arguments="{_all}" /> </f:else> </f:if>
注意事项
- 路径确认:请根据你使用的News扩展版本和样式包调整模板路径。如果未使用Bootstrap5样式,原模板路径通常为
EXT:news/Resources/Private/Templates/News/List.html。 - 变量传递:
arguments="{_all}"会将当前模板中的所有变量(如newsItems、settings等)传递给原模板,这是保证原模板正常工作的关键,不要省略。
内容的提问来源于stack exchange,提问作者fishbone
相关产品推荐
相关产品推荐

