TYPO3版本7.6.2-条件ViewHelpers仅评估一次 [英] TYPO3 ver. 7.6.2 - Condition ViewHelpers evaluated only once

查看:91
本文介绍了TYPO3版本7.6.2-条件ViewHelpers仅评估一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题::我写了一个条件VH(扩展了AbstractConditionViewHelper),它照常工作,无论如何,我意识到在非缓存版本中,它仅被评估一次.最初我以为是我的错误,但是检查了常见的<f:if>,问题是相同的:S

Problem: I wrote a conditional VH (extending AbstractConditionViewHelper) and it works as usually, anyway I realized that in non-cached version it is evaluated only once. Initialy I thought that's my bug, but checked common <f:if> and the problem is identical :S

通常,当我第一次访问我的页面时,会评估条件并给出有效的结果,但是当我刷新页面时,不再调用VH(通过在VH内设置断点进行检查),并且VH为始终被视为FALSE.视图代码中的任何更改都将导致对VH进行一次评估,并且下次刷新将不再调用VH.

In general when I visit my page for the first time, condition is evaluated and valid result is given, but when I'll refresh the page VH isn't called anymore (checked by setting breakpoint inside the VH) and VH is always treated as FALSE. Only any change in view's code will cause that VH will be evaluated once, and again next refresh(es) won't call VH anymore.

typo3conf/ext/toolbox/Classes/ViewHelpers/IsFieldRequiredViewHelper.php:

typo3conf/ext/toolbox/Classes/ViewHelpers/IsFieldRequiredViewHelper.php:

<?php
namespace Vendor\Toolbox\ViewHelpers;

class IsFieldRequiredViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper {

    /**
     * @param string $fieldName      Current field name
     * @param string $requiredFields List of required names separated by commas
     *
     * @return string the rendered string
     */
    public function render($fieldName, $requiredFields) {

        $requiredArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $requiredFields, true);

        return (in_array($fieldName, $requiredArray))
            ? $this->renderThenChild()
            : $this->renderElseChild();
    }
}

用法:

{namespace toolbox=Vendor\Toolbox\ViewHelpers}

<toolbox:isFieldRequired fieldName="foo" requiredFields="foo, bar, baz">
    <f:then>TRUE</f:then>
    <f:else>FALSE</f:else>
</toolbox:isFieldRequired>

对于第一个匹配,我有TRUE,但后来只有FALSE.

For the first hit I have TRUE but later only FALSE.

有什么建议吗?从7.x-开始,我是否错过了ViewHelpers API的一些重要更改?

Any suggestions? Did I missed some important change in ViewHelpers API since 7.x- ?

当然,如果扩展名已缓存,则它将不可见,因为第一个匹配项将以正确的VH返回值保存在缓存中.

Of course if extension is cached it will be not visible, as the first hit will be saved in cache with proper VH return.

推荐答案

AbstractConditionViewHelper实现TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface接口.这意味着它实现了一个compile方法,该方法实际上返回将存储在编译后的Fluid视图中的PHP代码.

The AbstractConditionViewHelper implements the TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface interface. This means that it implements a compile method that actually returns PHP code that will be stored in the compiled Fluid views.

一旦编译,将不再调用render()方法(当模板尚未编译时,它将在第一次调用时调用).相反,将调用renderStatic()方法.

Once compiled, the render() method will not be called anymore (it will on the first invocation, when the template is not yet compiled). Instead, the renderStatic() method will be called.

解决方案:您可以

  1. 还重写renderStatic()方法并在那里(再次)实现ViewHelper逻辑
  2. 不实现render()方法,而只是覆盖静态的evaluateCondition($arguments)方法.实际上,该方法被设计为可以被覆盖-render()renderStatic()的默认实现都调用此方法:

  1. also override the renderStatic() method and implement your ViewHelper logic there (again)
  2. not implement the render() method and simply overwrite the static evaluateCondition($arguments) method. This method is actually designed to be overwritten -- the default implementations of both render() and renderStatic() call this method:

此方法确定条件是TRUE还是FALSE.可以在扩展viewhelper中覆盖它以调整功能.

This method decides if the condition is TRUE or FALSE. It can be overriden in extending viewhelpers to adjust functionality.

static protected function evaluateCondition($arguments = null)
{
    $requiredArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $arguments['requiredFields'], true);
    return (in_array($arguments['fieldName'], $requiredArray));
}

这篇关于TYPO3版本7.6.2-条件ViewHelpers仅评估一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆