如何在 TYPO3 Fluid 中比较字符串与变量

4 typo3 fluid view-helpers typo3-9.x

我想做一个简单的比较,例如相当于:

if ($somevar === 'somestring')

现在,我找到了一些这样做的例子:

<f:if condition="{somevar} == 'somestring'"> 
...
Run Code Online (Sandbox Code Playgroud)

TYPO3 core 9.5 中也有很多地方,例如.

但官方文档告诉我们不然,我们必须使用基于数组的奇怪的解决方法:

不允许使用 XX/YY 处的字符串,但暂时可以通过比较数组来实现字符串比较

如果查看助手

由于实现复杂,Fluid 还无法与字符串进行比较,例如 ....。

外部基础/流体

不管什么意思 ...

为了避免问为什么:在 TYPO3 9 中比较字符串和变量的推荐方法是什么?从什么时候可以做到这一点?

小智 6

从 TYPO3 8.7 开始,Fluid if-ViewHelper 可以进行字符串、数字和数组比较,而无需将字符串放入数组中。但是,不支持通配符或正则表达式(如果您用 PHP 编写自定义 ViewHelper,那就没有限制)。

支持的比较运算符有:==、!=、<、<=、>、>= 和 %。还支持逻辑运算符 && 和 ||。您可以使用 ! 来否定布尔值。(比如!{启用})。

我听说文档团队正在努力更新和重组 Fluid 文档。同时,此页面有许多有用的提示和技巧:https://usetypo3.com/24-fluid-tips.html

示例(使用标签和内联语法的混合):

  1. 字符串比较:将变量与字符串进行比较
<f:variable name="foo">stuff</f:variable>
<f:if condition="{foo} == 'stuff'">
    <f:render partial="FooPartial" arguments="{foo: foo}" />
</f:if>
Run Code Online (Sandbox Code Playgroud)
  1. 比较整数(流畅的内联语法)
{f:variable(name: 'bar', value: 123)}
{f:variable(name: 'baz', value: 50)}
{f:if(condition: '{bar} > {baz}', then: 'This will print')}
Run Code Online (Sandbox Code Playgroud)
  1. 布尔运算符:&&、||
<f:if condition="{bar} > {baz} && {baz} < 100">This will print.</f:if>
Run Code Online (Sandbox Code Playgroud)
  1. 字符串“false”将计算为布尔值 false(流畅的内联语法)
{f:variable(name: 'untrue', value: 'false')}
{f:if(condition: untrue, then: 'Will not print', else: 'Will print')}
Run Code Online (Sandbox Code Playgroud)