TYPO3复合液(如果有条件) [英] TYPO3 Fluid complex if conditions
问题描述
如果条件不稳定,我会尝试编写以下内容,但它并没有按我希望的那样工作.
I am trying to write the following if condition in fluid but it is not working as I would hope.
条件 作为for循环的一部分,我要检查项目是第一个还是第4个,第8个,等等.
Condition As part of a for loop I want to check if the item is the first one or 4th, 8th etc
我本以为下面的方法会起作用,但是它会显示每次迭代的代码.
I would have thought the following would work but it display the code for every iteration.
<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle % 4} == 0">
我设法使它与嵌套if一起工作,但是两次使用同一段代码,并使用<f:else>
而不是== 0
I have managed to get it working with a nested if but it just feels wrong having the same section of code twice and also having the cycle check use a <f:else>
instead of == 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的答案.这是从克劳斯的答案中引用的:
Updated the answer for TYPO3 v8. This is quoted from Claus answer below:
根据当前情况更新此信息:
Updating this information with current situation:
在TYPO3v8和更高版本上,支持以下适合的语法 完全适合您的用例:
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>
在某些情况下(尤其是当使用 内联语法中的条件,您不能在其中扩展到标记模式 以便使用新的if参数访问f:else.
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条件(例如多个或/和组合),可以在your_extension/Classes/ViewHelpers/
中添加自己的ViewHelper.您只需要扩展Fluids AbstractConditionViewHelper
.随Fluid一起提供的简单if-ViewHelper看起来像这样:
For more complex if-Conditions (like several or/and combinations) you can add your own ViewHelper in your_extension/Classes/ViewHelpers/
. You just have to extend Fluids AbstractConditionViewHelper
. The simple if-ViewHelper that shipps with Fluid looks like this:
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并呈现否则,孩子.对于您的示例,您可以使用类似这样的内容:
All you have to do in your own ViewHelper is to add more parameter than $condition
, like $or
, $and
, $not
etc. Then you just write your if-Conditions in php and render either the then or else child. For your Example, you can go with something like this:
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. :
The File would be in your_extension/Classes/ViewHelpers/ExtendedIfViewHelper.php Then you have to add your namespace in the Fluid-Template like this (which enables all your self-written ViewHelpers from your_extension/Classes/ViewHelpers/ in the template:
{namespace vh=Vendor\YourExtension\ViewHelpers}
并按如下所示在您的模板中调用它:
and call it in your template like this:
<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复合液(如果有条件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!