Typo3 类型3流体复合体if条件

Typo3 类型3流体复合体if条件,typo3,fluid,Typo3,Fluid,我试图在流体中写下以下条件,但它并没有像我希望的那样工作 条件 作为for循环的一部分,我想检查该项是第一项还是第四项、第八项等等 我原以为下面的代码可以工作,但它显示了每个迭代的代码 <f:if condition="{logoIterator.isFirst} || {logoIterator.cycle % 4} == 0"> 我已经设法让它与嵌套的if一起工作,但它只是感觉两次使用相同的代码段是错误的,并且让循环检查使用而不是==0 <f:if condition

我试图在流体中写下以下条件,但它并没有像我希望的那样工作

条件 作为for循环的一部分,我想检查该项是第一项还是第四项、第八项等等

我原以为下面的代码可以工作,但它显示了每个迭代的代码

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

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

做点什么
做点什么

是的,感觉不对劲,但这是唯一的办法。这是一个非常适合viewhelper的网站::

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}">
    <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>
您在自己的ViewHelper中所要做的就是添加比
$condition
更多的参数,如
$or
$和
$not
等。然后您只需在php中编写if条件,并呈现Then或else子级。例如,您可以使用以下内容:

<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4">
    Is first or n4th
</f:if>
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();
        }
    }
}
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();
        }
    }
}
<vh:extendedIf condition="{logoIterator.isFirst}" or="{logoIterator.cycle} % 4">
  <f:then>Do something</f:then>
  <f:else>Do something else</f:else>
</vh:extendedIf>
<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4">
    Is first or n4th
</f:if>
并在模板中这样调用它:

<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4">
    Is first or n4th
</f:if>
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();
        }
    }
}
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();
        }
    }
}
<vh:extendedIf condition="{logoIterator.isFirst}" or="{logoIterator.cycle} % 4">
  <f:then>Do something</f:then>
  <f:else>Do something else</f:else>
</vh:extendedIf>
<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4">
    Is first or n4th
</f:if>

做点什么
做点别的
编辑:已更新。

您还可以使用以下提供的:


{logoIterator.isFirst}|{logoIterator.cycle%4}==0
如果为真,则输出
如果为FALSE,则输出

另一方面:VHS扩展提供了许多有用的ViewHelper。我觉得TYPO3 Fluid中应该包含很多。在vhs V2.0中,v:if.condition将被弃用 使用v:if堆栈代替:

除了Daniels的回答之外,我还制作了一个ViewHelper,它可以接受多种条件,包括“和”模式(默认)或“或”模式:


虽然它可以写得更好,但它很有效。 以下是我如何使用它:

{namespace vhs=TLID\contentelements\ViewHelpers}

<vhs:if checks="{0: settings.link}">
  <f:comment><!-- store the content --></f:comment>
  <store>
    <f:if condition="{images}">
      <f:for each="{images}" as="image">
        <f:image image="{image}" alt="{image.description}" title="{image.title}" />
      </f:for>
    </f:if>

    <vhs:if checks="{0: settings.headline, 1: settings.text}" type="or">
      <success>
        <div>
          <f:if condition="{settings.headline}"><h2><f:format.nl2br><vhs:shy>{settings.headline}</vhs:shy></f:format.nl2br></h2></f:if>
          <f:if condition="{settings.text}"><p><f:format.nl2br><vhs:shy>{settings.text}</vhs:shy></f:format.nl2br></p></f:if>
        </div>        
      </success>
    </vhs:if>
  </store>

  <f:comment><!-- use the content of this container on success --></f:comment>
  <success>
    <vhs:link href="{settings.link}" target="{settings.target}" class="box">
      <use />
    </vhs:link>
  </success>

  <f:comment><!-- use the content of this container on failure --></f:comment>
  <failure>
    <div class="box">
      <use />
    </div>
  </failure>
</vhs:if>
{namespace vhs=TLID\contentelements\ViewHelpers}
{settings.headline}
{settings.text}


它还有一个store元素,因为我不喜欢它编写两次相同的代码。因此,您可以选择保存一些流体,并将其传递到成功容器和失败容器,而无需重复。

对于我来说,使用“f:cycle”的最佳方法。如果每个第3个元素的行都需要Device,我只需执行以下操作:

<v:variable.set  name="wraper" value='</div><div class="row">' />
<f:for each="{items}" as="item" iteration="itemIterator">
 ....
   <f:cycle values="{0: '', 1: '', 2: '{wraper}'}" as="cycle">
        {cycle -> f:format.raw()}
  </f:cycle>
 ...
</f:for>

....
{cycle->f:format.raw()}
...

使用当前情况更新此信息:

在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}">
    <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>

弗斯特
N4
N8
不是第一个,不是第四个,不是第八个-回退/正常
此外,还支持以下语法:

<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4">
    Is first or n4th
</f:if>
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();
        }
    }
}
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();
        }
    }
}
<vh:extendedIf condition="{logoIterator.isFirst}" or="{logoIterator.cycle} % 4">
  <f:then>Do something</f:then>
  <f:else>Do something else</f:else>
</vh:extendedIf>
<f:if condition="{logoIterator.isFirst} || {logoIterator.cycle} % 4">
    Is first or n4th
</f:if>

是第一还是第四

在某些情况下(特别是在使用内联语法中的条件时,您不能扩展到标记模式,以便使用新的
if
参数访问
f:else

对于许多情况,使用数组比较就足够了,因此您不必创建自定义视图辅助对象



遗憾的是,这只是为了检查是否设置了变量,而不是检查值。但在许多情况下,这就足够了


PS:(通过此数组比较,您还可以比较字符串)

如果您有CObjects,请帮助解决逻辑OR:

# Sidebar | 1 ColPos = 78
lib.sidebar1 < styles.content.get
lib.sidebar1.select.where = colPos=78
# Sidebar | 2 ColPos = 79
lib.sidebar2 < styles.content.get
lib.sidebar2.select.where = colPos=79

#LogicalOR
lib.tempLogicalOrSidebar = COA
lib.tempLogicalOrSidebar {
    10 < lib.sidebar1
    10.stdWrap.override.cObject =< lib.sidebar2
}
#侧栏| 1 ColPos=78
lib.sidebar1
液体如果条件:

<f:if condition="{f:cObject(typoscriptObjectPath: 'lib.tempLogicalOrSidebar.10')}">

通过f:if、v:variable.set和v:math的组合,可以实现复杂的if条件。使用math ViewHelper执行此操作并将其结果存储在变量中。然后使用if比较器对其进行验证和操作

以下是我的示例代码:

<f:for each="{customers}" as="customer" iteration="iterator">
    <v:variable.set name="isFirst" value="{v:math.modulo(a: iterator.cycle, b: settings.itemsperrow, fail: 0)}" />
    <f:if condition="{isFirst}==1">
        <div class="row">
    </f:if>
    <div class="col-md-{settings.colWidth}">
        <div class="clientlogo_ref">
            <f:image src="{customer.logo.originalResource.publicUrl}" />
        </div>                 
    </div>
    <f:if condition="{isFirst}==0">
        </div>
    </f:if>
</f:for>

此代码开始/结束由settings.ItemsError定义的每个X项的网格行。这是可变的,可以在插件的配置中设置。它使用模来计算iterator.cycle(计数器以1开头)mod settings.itemsperrow。如果结果为1,则它是行的第一个元素。0表示它是最后一行,因此必须关闭该行。

2017年状态:

下面是使用堆栈属性的现代VHS viewhelper条件的示例。此示例还包含一个空检查和一个逻辑or(
|
),以显示如何执行此操作

<v:if stack="{0: '{itemId}', 1:'==', 2:NULL, 3: '||', 4: '{itemId}', 5: '==', 6: '{falMedia.uid}'}">
    <f:then>
    ...
    </f:then>
</v:if>

...

请注意,
NULL
没有被引用

谢谢你的回答。我想知道是否有人考虑过或组织一个视图助手来处理更复杂的条件?不要再为TYPO3和Fluid辩护了。遗憾的是,您需要变通方法来处理布尔条件,这令人尴尬。TYPO3是用PHP编写的。为什么不使用它呢<代码>
不完整,
不完整。它是完整的。成熟,测试,使用多年,被数百万开发者使用。在解析流畅的语法以显示网站时,接受这种额外的开销是愚蠢的。如果需要,视图仍然可以与逻辑分离,同时使用相同的语言保留MVC概念。感谢帮助者的帮助。我的例子将在4号做一些事情,因为它使用了else,然后是当有一个使人困惑的余数时