PHP Storm 验证的第一件事是正确嵌套 XML/HTML - 类似标签,在您的示例中,您无情地破坏了它(从 IDE 的角度来看,不是我的,因为我理解需要使用模 break 在 Fluid 中),添加有效的 XML 命名空间甚至完全禁用 XML 检查器都没有关系。
更聪明甚至可以在内联then通知中识别标签(该死!),这也会显示IDE级别错误:
<ul>
<f:for each="{items}" as="item" iteration="i">
<li>Item {i.cycle}</li>
{f:if(condition: '{i.cycle} % 4', then: '</ul><ul>')}
</f:for>
</ul>
到目前为止,我发现并在许多项目中成功使用它的唯一解决方案是自定义 ext 中的自定义 ViewHelper(对于 IDE,在 endTag 之前声明 startTage 很重要!):
<?php
namespace Vendor\Myext\ViewHelpers;
/**
* {namespace myNs=Vendor\Myext\ViewHelpers}
*
* = Inline samples
*
* === Break the list:
* {myNs:breakTagByModulo(iterator: i, modulo: 2)}
* or...
* {myNs:breakTagByModulo(iterator: i, modulo: 2, startTag: '<ul class="next-level">', endTag: '</ul>')}
*
* result on valid modulo:
* </ul><ul class="next-level">
*
* === Break the Bootstrap row:
* {myNs:breakTagByModulo(iterator: i, modulo: 2, startTag: '<div class="row">', endTag: '</div>')}
*
* result on valid modulo:
* </div><div class="row">
*
* etc...
*/
class BreakTagByModuloViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* @param array $iterator Iterator from `f:for` VH
* @param integer $modulo Modulo to check
* @param boolean $skipLast If skipLast==true VH will return false even if condition is correct, needed for `not full` lists
* @param string $startTag Begin of the tag
* @param string $endTag End of the tag
*
* @return bool|string
*/
public function render($iterator, $modulo, $skipLast = true, $startTag = '<ul>', $endTag = '</ul>') {
$i = $iterator['cycle'];
$bool = ($i % $modulo == 0);
if ($skipLast && $iterator['isLast']) {
$bool = false;
}
return ($bool) ? $endTag . $startTag : null;
}
}
?>
视图中的用法如上所示,仅对于您的示例,它将类似于:
{namespace myNs=Vendor\Myext\ViewHelpers}
<ul>
<f:for each="{items}" as="item" iteration="i">
<li>(CONTENT HERE)</li>
{myNs:breakTagByModulo(iterator: i, modulo: 4)}
</f:for>
</ul>
(当然你要用自己的值替换 myNs、Vendor 和 Myext)