TYPO3 v8
更新了 TYPO3 v8 的答案。这是从克劳斯的回答下面引用的:
根据当前情况更新此信息:
在 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>
此外还支持这样的语法:
<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4">
Is first or n4th
</f:if>
这可能更适合某些情况(尤其是在使用
内联语法中的条件,您无法在其中扩展为标记模式
以便使用新的 if 参数访问 f:else)。
TYPO3 6.2 LTS 和 7 LTS
对于更复杂的 if 条件(例如多个或/和组合),您可以在 your_extension/Classes/ViewHelpers/ 中添加自己的 ViewHelper。你只需要扩展流体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();
}
}
}
您在自己的 ViewHelper 中所要做的就是添加比 $condition 更多的参数,例如 $or、$and、$not 等。然后您只需在 php 中编写 if-Conditions 并渲染那么要不然孩子。对于您的示例,您可以使用以下内容:
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();
}
}
}
该文件将位于 your_extension/Classes/ViewHelpers/ExtendedIfViewHelper.php 然后您必须像这样在 Fluid-Template 中添加您的命名空间(这将启用模板中 your_extension/Classes/ViewHelpers/ 中的所有您自己编写的 ViewHelpers :
{namespace vh=Vendor\YourExtension\ViewHelpers}
并像这样在您的模板中调用它:
<vh:extendedIf condition="{logoIterator.isFirst}" or="{logoIterator.cycle} % 4">
<f:then>Do something</f:then>
<f:else>Do something else</f:else>
</vh:extendedIf>
编辑:更新。