视图助手中缺少子节点
Missing child nodes in view helper
我创建了一个视图助手,它曾经在 TYPO3 7.6 上工作,但在 TYPO3 8.7 上停止工作。它始终呈现 else 部分,即使 evaluateCondition
的 return 值在调试时是正确的。
在调试时,我看到 AbstractConditionViewHelper
中的 AbstractConditionViewHelper
被调用,然后 renderThenChild
或 renderElseChild
被调用。但是那里缺少子节点。
我的视图助手:
<?php
namespace Vendor\Extkey\ViewHelpers;
class IfRegExpViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper
{
/**
* Initialize arguments
* @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
*/
public function initializeArguments() {
$this->registerArgument('then', 'mixed', 'Value to be returned if the condition if met.', false);
$this->registerArgument('else', 'mixed', 'Value to be returned if the condition if not met.', false);
$this->registerArgument('value', 'string', 'The value', TRUE);
$this->registerArgument('pattern', 'string', 'The regex pattern to check', TRUE);
}
/**
* Check if pattern matches
*
* @param array $arguments ViewHelper arguments to evaluate the condition for this ViewHelper, allows for flexiblity in overriding this method.
* @return string the rendered string
* @api
*/
protected static function evaluateCondition($arguments = NULL)
{
if (preg_match($arguments['pattern'], $arguments['value']) === 1) {
return true;
} else {
return false;
}
}
}
模板中的用法:
<f:if condition="{eddaylight:IfRegExp(value: overview, pattern: '/[a-zA-Z0-9]+/')}">
<f:then>
{overview}
</f:then>
<f:else>
something else
</f:else>
</f:if>
我需要做些什么来解析子节点吗?
<f:if condition="{eddaylight:IfRegExp(value: overview, pattern: '/[a- zA-Z0-9]+/')}">
<f:then>
{overview}
</f:then>
<f:else>
something else
</f:else>
</f:if>
是错误的 - 您编写的 ViewHelper 是一个条件 ViewHelper 本身,因此它 returns 是 then
或 else
node/closure/argument 的值并且如果 none 被指定,没有返回。你上面写的是一样的:
<f:if condition="">
这显然是错误的。
ViewHelper 的使用方法是:
<eddaylight:ifRegExp value="{overview}" pattern="/[a-zA-Z0-9]+/">
<f:then>
{overview}
</f:then>
<f:else>
something else
</f:else>
</eddaylight:ifRegExp>
同样适用于 AbstractConditionViewHelper
的所有其他子类。
关于子节点的注意事项:
- 在您当前的用例中,没有子节点是正确的,因为您使用内联符号编写 ViewHelper 而没有传递子值。
- 如果您扩展到正确的语法,您将看到子节点,但只有在模板被编译之前,子节点将不再被设置。
我创建了一个视图助手,它曾经在 TYPO3 7.6 上工作,但在 TYPO3 8.7 上停止工作。它始终呈现 else 部分,即使 evaluateCondition
的 return 值在调试时是正确的。
在调试时,我看到 AbstractConditionViewHelper
中的 AbstractConditionViewHelper
被调用,然后 renderThenChild
或 renderElseChild
被调用。但是那里缺少子节点。
我的视图助手:
<?php
namespace Vendor\Extkey\ViewHelpers;
class IfRegExpViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper
{
/**
* Initialize arguments
* @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
*/
public function initializeArguments() {
$this->registerArgument('then', 'mixed', 'Value to be returned if the condition if met.', false);
$this->registerArgument('else', 'mixed', 'Value to be returned if the condition if not met.', false);
$this->registerArgument('value', 'string', 'The value', TRUE);
$this->registerArgument('pattern', 'string', 'The regex pattern to check', TRUE);
}
/**
* Check if pattern matches
*
* @param array $arguments ViewHelper arguments to evaluate the condition for this ViewHelper, allows for flexiblity in overriding this method.
* @return string the rendered string
* @api
*/
protected static function evaluateCondition($arguments = NULL)
{
if (preg_match($arguments['pattern'], $arguments['value']) === 1) {
return true;
} else {
return false;
}
}
}
模板中的用法:
<f:if condition="{eddaylight:IfRegExp(value: overview, pattern: '/[a-zA-Z0-9]+/')}">
<f:then>
{overview}
</f:then>
<f:else>
something else
</f:else>
</f:if>
我需要做些什么来解析子节点吗?
<f:if condition="{eddaylight:IfRegExp(value: overview, pattern: '/[a- zA-Z0-9]+/')}">
<f:then>
{overview}
</f:then>
<f:else>
something else
</f:else>
</f:if>
是错误的 - 您编写的 ViewHelper 是一个条件 ViewHelper 本身,因此它 returns 是 then
或 else
node/closure/argument 的值并且如果 none 被指定,没有返回。你上面写的是一样的:
<f:if condition="">
这显然是错误的。
ViewHelper 的使用方法是:
<eddaylight:ifRegExp value="{overview}" pattern="/[a-zA-Z0-9]+/">
<f:then>
{overview}
</f:then>
<f:else>
something else
</f:else>
</eddaylight:ifRegExp>
同样适用于 AbstractConditionViewHelper
的所有其他子类。
关于子节点的注意事项:
- 在您当前的用例中,没有子节点是正确的,因为您使用内联符号编写 ViewHelper 而没有传递子值。
- 如果您扩展到正确的语法,您将看到子节点,但只有在模板被编译之前,子节点将不再被设置。