如果条件不流畅,我试着写下面的内容,但它没有按照我希望的方式工作.
条件 作为for循环的一部分,我想检查项目是第一个还是第4个,第8个等
我会认为以下方法可行,但它会显示每次迭代的代码.
<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle % 4} == 0">
Run Code Online (Sandbox Code Playgroud)
我已经设法让它使用嵌套if,但只是感觉错了两次相同的代码段并且循环检查使用a <f:else>而不是== 0
<f:if condition="{logoIterator.isFirst}">
<f:then>
Do Something
</f:then>
<f:else>
<f:if condition="{logoIterator.cycle} % 4">
<f:else>
Do Something
</f:else>
</f:if>
</f:else>
</f:if>
Run Code Online (Sandbox Code Playgroud)
Dan*_*iel 19
TYPO3 v8
更新了TYPO3 v8的答案.这是克劳斯的回答,引用如下:
使用当前情况更新此信息:
在TYPO3v8及更高版本中,支持以下语法,完全符合您的用例:
Run Code Online (Sandbox Code Playgroud)<f:if condition="{logoIterator.isFirst}"> <f:then>First</f:then> <f:else if="{logoIterator.cycle % 4}">n4th</f:else> <f:else if="{logoIterator.cycle % 8}">n8th</f:else> <f:else>Not first, not n4th, not n8th - fallback/normal</f:else> </f:if>另外还支持这样的语法:
Run Code Online (Sandbox Code Playgroud)<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4"> Is first or n4th </f:if>对于某些情况,这可能更合适(特别是在内联语法中使用条件时,您无法扩展到标记模式以便使用新的if参数访问f:else).
TYPO3 6.2 LTS和7 LTS
对于更复杂的if-Conditions(如几个或/和组合),您可以添加自己的ViewHelper your_extension/Classes/ViewHelpers/.你只需要扩展流体AbstractConditionViewHelper.使用Fluid进行简单的if-ViewHelper看起来像这样:
class IfViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper {
/**
* renders <f:then> child if $condition is true, otherwise renders <f:else> child.
*
* @param boolean $condition View helper condition
* @return string the rendered string
* @api
*/
public function render($condition) {
if ($condition) {
return $this->renderThenChild();
} else {
return $this->renderElseChild();
}
}
}
Run Code Online (Sandbox Code Playgroud)
所有你必须在自己的视图助手做的是增加更多的参数比$condition,像$or,$and,$not等等,那么你只写你如果-条件在PHP和渲染无论是当时还是别的孩子.对于您的示例,您可以使用以下内容:
class ExtendedIfViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper {
/**
* renders <f:then> child if $condition or $or is true, otherwise renders <f:else> child.
*
* @param boolean $condition View helper condition
* @param boolean $or View helper condition
* @return string the rendered string
*/
public function render($condition, $or) {
if ($condition || $or) {
return $this->renderThenChild();
} else {
return $this->renderElseChild();
}
}
}
Run Code Online (Sandbox Code Playgroud)
文件将在your_extension/Classes/ViewHelpers/ExtendedIfViewHelper.php中然后您必须在Fluid-Template中添加您的命名空间(这样可以从模板中的your_extension/Classes/ViewHelpers /启用所有自编的ViewHelpers:
{namespace vh=Vendor\YourExtension\ViewHelpers}
Run Code Online (Sandbox Code Playgroud)
并在您的模板中调用它,如下所示:
<vh:extendedIf condition="{logoIterator.isFirst}" or="{logoIterator.cycle} % 4">
<f:then>Do something</f:then>
<f:else>Do something else</f:else>
</vh:extendedIf>
Run Code Online (Sandbox Code Playgroud)
编辑:更新.
Cla*_*Due 11
使用当前情况更新此信息:
在TYPO3v8及更高版本中,支持以下语法,完全符合您的用例:
<f:if condition="{logoIterator.isFirst}">
<f:then>First</f:then>
<f:else if="{logoIterator.cycle % 4}">n4th</f:else>
<f:else if="{logoIterator.cycle % 8}">n8th</f:else>
<f:else>Not first, not n4th, not n8th - fallback/normal</f:else>
</f:if>
Run Code Online (Sandbox Code Playgroud)
另外还支持这样的语法:
<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4">
Is first or n4th
</f:if>
Run Code Online (Sandbox Code Playgroud)
对于某些情况,这可能更合适(特别是在内联语法中使用条件时,您无法扩展到标记模式以获取f:else对新if参数的访问权限).