如何检查(字符串)是否等于(数组内的字符串)?我可以用If语句检查它吗?

时间:2019-05-13 08:39:14

标签: arrays if-statement typo3 fluid

我要检查字符串是否匹配数组中的字符串之一。

使用TYPO3流体或使用vhs是否可能?

<f:if condition="{string} ?? {anArrayOfStrings}">
  <f:then>
    <p>Message</p>
  </f:then>
  <f:else>

  </f:else>
</f:if>

非常感谢:)

4 个答案:

答案 0 :(得分:0)

默认情况下,Fluid没有inArray ViewHelper,但VHS拥有(v:if.iterator.contains)。参见https://fluidtypo3.org/viewhelpers/vhs/1.8.2/If/Iterator/ContainsViewHelper.html

如果要在没有VHS的情况下执行此操作,则可以创建自定义ViewHelper。您可以在这里https://www.andrerinas.de/tutorials/typo3-in-array-viewhelper.html上找到更多信息(德语),但是即使您不懂德语,代码也应该足够清楚

答案 1 :(得分:0)

TYPO3没有这样的ViewHelper。请使用EXT:vhs的ViewHelper

https://fluidtypo3.org/viewhelpers/vhs/1.8.2/If/Iterator/ContainsViewHelper.html

{namespace v=FluidTYPO3\Vhs\ViewHelpers}
<v:if needle="{string}" haystack="{anArrayOfStrings}">
  <f:then>
    <p>Message</p>
  </f:then>
  <f:else>

  </f:else>
</v:if>

不知道这是否适用于f:than。也许尝试使用v:than或此ViewHelper的than-Attribute。

答案 2 :(得分:0)

我使用了VHS“ v:condition.string.contains”。它有效:)

<v:condition.string.contains needle="{string}" haystack="{anArrayOfStrings}">
  <f:then>
    Message
  </f:then>
  <f:else>
  </f:else>
</v:condition.string.contains>

答案 3 :(得分:0)

这是一个适用于TYPO3 9.5.14的具体示例。根据选定的截面框架(“ custom-45”或“ custom-50”),流体使用类“ my-custom-frame”渲染特殊的div元素,否则将使用默认渲染:

<v:condition.string.contains needle="{data.frame_class}" haystack="{'custom-45','custom-50'}">
    <f:then>
        <div class="my-custom-frame">
    </f:then>
    <f:else>
        <div id="c{data.uid}" class="frame frame-{data.frame_class} frame-type-{data.CType} frame-layout-{data.layout}{f:if(condition: data.space_before_class, then: ' frame-space-before-{data.space_before_class}')}{f:if(condition: data.space_after_class, then: ' frame-space-after-{data.space_after_class}')}">
    </f:else>
</v:condition.string.contains>

针是内容元素的选定截面框架。在大海捞针中,您可以找到导致真实状况的值。我用它来缩短or语法:

<f:if condition="{data.frame_class} == 'custom-45' || {data.frame_class} == 'custom-50'">
    <f:then>
        <div class="my-custom-frame">
    </f:then>
    <f:else>
        <div id="c{data.uid}" class="frame frame-{data.frame_class} frame-type-{data.CType} frame-layout-{data.layout}{f:if(condition: data.space_before_class, then: ' frame-space-before-{data.space_before_class}')}{f:if(condition: data.space_after_class, then: ' frame-space-after-{data.space_after_class}')}">
    </f:else>
</f:if>

在流体模板中不需要名称空间声明。使用的VHS:Fluid ViewHelpers-Extension为6.0.0。

相关问题