带有通配符或子上下文的 TypoScript applicationContext 条件是不可能的

TypoScript applicationContext condition with wildcards or sub-contexts not possible

由于 TYPO3 9.5 LTS 建议对 TypoScript 条件使用 Symfony 表达式语言。

我在迁移它们时遇到了一些问题:

旧语法:

// Matches any applicationContext with a rootContext of "Production", for example "Production/Live" or "Production/Staging":
[applicationContext = Development*]

// Matches any applicationContext starting with "Production/Dev"
[applicationContext = /^Production\/Dev/]

到新的表达语言:

我尝试了以下但没有成功:

[applicationContext == "/^Development\/Docker/"]

[applicationContext == "Development*"]

[applicationContext == "Development/*"]

我没有找到任何示例。我不确定条件测试是否已经基于表达式语言。 (https://review.typo3.org/#/c/57787/)

如果有人能建议如何使用该功能来像以前一样添加条件,那就太好了

您需要使用 Symfony 表达式语言的 matches comparison operator。这样你就可以使用正则表达式进行部分匹配:

[applicationContext matches "/^Development/"]

这将匹配任何 Development 上下文。

以下条件匹配任何以“Production/Dev”开头的applicationContext

[applicationContext matches '/^Production\\\\/Dev/']

Why so many backslashes? A backslash (\) must be escaped by 4 backslashes (\\) in a string and 8 backslashes (\\\\) in a regex in the Symfony Expression Language

(Symfony Expression Language acts as the base for TypoScript conditions since TYPO3 9 LTS)


EDIT 2020-02: Using a different regex delimiter makes the readability much better for applicationContext conditions within TYPO3:

Instead of / we can use # as regular expression delimiters.

Here are some examples:

匹配“Development”applicationContext

[applicationContext matches "#^Development#"]

匹配以“Production/Dev”开头的applicationContext

[applicationContext matches "#^Production/Dev#"]

匹配以“Production/DevProduction/Staging”开头的applicationContext

[applicationContext matches "#^Production/(Dev|Staging)#"]

匹配以“Production/Live

开头的applicationContext
[applicationContext matches "#^Production/Live#"]

匹配以“Production/Live 开头的 applicationContext 和活动的 TYPO3 后端用户会话

[applicationContext matches "#^Production/Live#" && getTSFE().isBackendUserLoggedIn()]

匹配除以“Production

开头的任何applicationContext
[not (applicationContext matches "#Production#")]

匹配 applicationContext 与值“Production/Live/ClusterServer1

[applicationContext == "Production/Live/ClusterServer1"]

在 TYPO3 9 中你可以[applicationContext = Development/*]

此正则表达式匹配准确的字符串“Production”和任何以“Production/”开头的字符串,例如“Production/Staging”。

匹配 applicationContext 是“Production”或以“Production/”开头

[applicationContext matches '#^Production(/|$)#']