TYPO3 v11中如何在TypoScript条件中调用Setup变量?
在TYPO3 v11的TypoScript条件中调用Setup变量的解决方案
直接在TypoScript条件中引用Setup层的对象(比如你的lib.test)不可行,因为TypoScript的执行顺序是:条件判断阶段先于Setup层解析,条件执行时Setup里的内容还没被处理,自然拿不到对应的值。
以下是三种可行的实现方法:
方法1:通过config.registerGlobals注册全局变量
把Setup里的变量值存入TSFE的register数组,条件阶段可以直接访问这个全局数组:
lib.test = TEXT lib.test.value = 1 # 将lib.test的值注册到TSFE->register['testVar'] config.registerGlobals.tsfe.register.testVar = TEXT config.registerGlobals.tsfe.register.testVar.data = lib.test # 条件中调用全局变量判断 [TSFE.register.testVar == 1] page.10.variables.CONTENT = TEXT page.10.variables.CONTENT.value = done! [END]
方法2:用常量+Setup覆盖的变通方案
如果变量值可以提前定义或动态覆盖,可先在Constants层声明,再在Setup层修改,条件中直接调用常量(此时会取到Setup覆盖后的值):
# Constants 层 test.value = 0 # Setup 层 test.value = 1 lib.test = TEXT lib.test.value = {$test.value} # 条件中使用常量判断(实际取到的是Setup覆盖后的值) [{$test.value} == 1] page.10.variables.CONTENT = TEXT page.10.variables.CONTENT.value = done! [END]
方法3:自定义UserFunc条件(适合复杂场景)
编写PHP函数直接访问解析后的Setup内容,实现自定义判断逻辑:
- 先在扩展中创建UserFunc类:
<?php namespace Vendor\YourExtension\UserFunctions; class ConditionCheck { public function checkLibTestValue(\TYPO3\CMS\Core\TypoScript\ConditionMatching\ConditionMatcher $conditionMatcher): bool { $tsfe = $GLOBALS['TSFE'] ?? null; if (!$tsfe) { return false; } // 从解析后的Setup中获取lib.test的value值 $libTestValue = $tsfe->tmpl->setup['lib.']['test.']['value'] ?? ''; return $libTestValue === '1'; } }
- 在TypoScript中调用这个自定义条件:
lib.test = TEXT lib.test.value = 1 [userFunc(Vendor\YourExtension\UserFunctions\ConditionCheck->checkLibTestValue)] page.10.variables.CONTENT = TEXT page.10.variables.CONTENT.value = done! [END]
内容的提问来源于stack exchange,提问作者user3852729
相关产品推荐
相关产品推荐

