您需要用逗号分解字符串,这样您就可以在循环中迭代它们,至少有两种方法:一种是自定义 ViewHelper,第二种(如下所述)是短暂的模型中的字段,当您获得特征的 ID 时,您还需要将其“翻译”为人类可读的标签......
在包含特征的模型中,添加瞬态场withgetter 如下例所示:(当然你可以删除注释中这些无聊的注释,但是你must保持@var
与正确的类型一致!):
/**
* This is a transient field, that means, that it havent
* a declaration in SQL and TCA, but allows to add the getter,
* which will do some special jobs... ie. will explode the comma
* separated string to the array
*
* @var array
*/
protected $featuresDecoded;
/**
* And this is getter of transient field, setter is not needed in this case
* It just returns the array of IDs divided by comma
*
* @return array
*/
public function getFeaturesDecoded() {
return \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->features, true);
}
如前所述,您需要获取人类可读的 ID 标签,即使您不构建多语言页面翻译文件也非常有用,只需在文件中typo3conf/ext/yourext/Resources/Private/Language/locallang.xlf
为 TCA 选择中的每个功能添加项目:
<trans-unit id="features.parking">
<source>Parkplätze</source>
</trans-unit>
<trans-unit id="features.wlan">
<source>Freies Wlan</source>
</trans-unit>
正如您所看到的,点后的反式单元的 id 与 TCA 中的特征键相同,
最后,在视图中使用它所需要的只是迭代短暂的字段而不是原始字段:
<f:for each="{newsItem.featuresDecoded}" as="feature">
<li>
Feature with key <b>{feature}</b>
it's <b>{f:translate(key: 'features.{feature}')}</b>
</li>
</f:for>
将新字段添加到模型中(即使它们是暂时的)并在本地化文件中添加或更改条目后,您需要清除系统缓存!在 6.2+ 中,此选项在安装工具中可用(但您可以通过 UserTS 在闪光图标).
注意:使用自定义 ViewHelper 可以完成同样的事情,但恕我直言,瞬态字段更容易。