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

在Typo3 Fluid中检查多字符串匹配是否有更优实现方式?

优化实现方案

你可以先用v:format.path.info ViewHelper直接提取文件的扩展名,再配合Fluid原生的f:switch实现判断,完全不需要多层嵌套,代码可读性和维护性都更高:

<f:for each="{event.downloadLinks}" as="downloadLink">
  <f:link.typolink parameter="{downloadLink.link}" target="_blank" title="{downloadLink.title}">{downloadLink.title}</f:link.typolink>
  <f:variable name="pathFile" value="{f:link.typolink(parameter: '{downloadLink.link}')}" />
  <!-- 直接提取文件扩展名 -->
  <f:variable name="fileExt" value="{v:format.path.info(path: pathFile, segment: 'extension')}" />
  <!-- 用switch替代多层嵌套判断 -->
  <f:switch expression="{fileExt}">
    <f:case value="jpg">JPG ICON</f:case>
    <f:case value="svg">SVG ICON</f:case>
    <f:case value="pdf">PDF ICON</f:case>
    <f:defaultCase>SOME OTHER ICON</f:defaultCase>
  </f:switch>
</f:for>

这种写法还有个额外优势:不会误判文件名中间包含后缀字符串的异常场景,比如文件名是test.jpg.pdf,用包含判断会误识别为jpg,取扩展名则可以正确识别为pdf。
如果需要兼容大小写后缀的情况,比如.JPG/.Pdf,可以加个转小写的处理:

<f:variable name="fileExt" value="{v:format.path.info(path: pathFile, segment: 'extension') -> v:format.lowercase()}" />

原实现效果参考:
效果示例图

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 09:27:01