gpt4 book ai didi

TYPO3 Fluid complex if条件

转载 作者:行者123 更新时间:2023-12-04 00:01:08 28 4
gpt4 key购买 nike

如果条件处于流体状态,我正在尝试编写以下内容,但它没有像我希望的那样工作。

条件
作为 for 循环的一部分,我想检查该项目是第一个还是第 4 个、第 8 个等

我原以为以下内容会起作用,但它会显示每次迭代的代码。

<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle % 4} == 0">

我已经设法让它与嵌套的 if 一起工作,但感觉两次相同的代码部分是错误的,并且循环检查使用 <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>

最佳答案

TYPO3 v8

更新了 TYPO3 v8 的答案。这是从下面的克劳斯回答中引用的:

Updating this information with current situation:

On TYPO3v8 and later, the following syntax is supported which fits perfectly with your use case:

<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>

In addition there is support for syntax like this:

<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4">
Is first or n4th
</f:if>

Which can be more appropriate for some cases (in particular when using a condition in inline syntax where you can't expand to tag mode in order to gain access to the f:else with the new if argument).



TYPO3 6.2 LTS 和 7 LTS

对于更复杂的 if-Conditions(如多个或/和组合),您可以在 your_extension/Classes/ViewHelpers/ 中添加您自己的 ViewHelper .您只需要扩展 Fluids 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 并呈现 then 或 else 子项。对于您的示例,您可以使用以下方法:
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>

编辑:更新。

关于TYPO3 Fluid complex if条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19731150/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com