首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TYPO3 ver.7.6.2 -条件ViewHelpers只评估一次

TYPO3 ver.7.6.2 -条件ViewHelpers只评估一次
EN

Stack Overflow用户
提问于 2016-01-11 18:59:23
回答 2查看 1.4K关注 0票数 5

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

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

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

代码语言:javascript
复制
<?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();
    }
}

用法:

代码语言:javascript
复制
{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

有什么建议吗?我是否错过了7.x-之后ViewHelpers API中的一些重要变化?

当然,如果缓存扩展,它将不可见,因为第一个命中将保存在缓存中,并返回适当的VH。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-12 12:16:58

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

查看source code中的此方法

代码语言:javascript
复制
 public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler)
 {
     foreach ($syntaxTreeNode->getChildNodes() as $childNode) {
         if ($childNode instanceof ViewHelperNode
             && $childNode->getViewHelperClassName() === ThenViewHelper::class) {
             $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
             $initializationPhpCode .= sprintf('%s[\'__thenClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF;
         }
         if ($childNode instanceof ViewHelperNode
             && $childNode->getViewHelperClassName() === ElseViewHelper::class) {
             $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
             $initializationPhpCode .= sprintf('%s[\'__elseClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF;
         }
     }

     return sprintf('%s::renderStatic(%s, %s, $renderingContext)',
         get_class($this), $argumentsVariableName, $renderChildrenClosureVariableName);
 }

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

解决方案:您可以

  1. 还重写renderStatic()方法并在那里实现您的ViewHelper逻辑(再次)
  2. render() 没有实现方法,只是简单地覆盖了静态evaluateCondition($arguments)方法。此方法实际上是设计为被覆盖的-- render()renderStatic()的默认实现都调用此方法: 这个方法决定条件是真是假。它可以在扩展越野车来调整功能。

静态保护函数evaluateCondition($arguments = null) { $requiredArray =$requiredArray ',$$requiredArray‘’requiredFields‘,true);返回(in_array($in_array’‘fieldName’,$requiredArray));}

票数 10
EN

Stack Overflow用户

发布于 2018-07-13 09:40:18

最快的解决方案是覆盖类呈现和evaluateCondition,如下所示:

代码语言:javascript
复制
public function initializeArguments()
   {
        parent::initializeArguments();
        $this->registerArgument('yourArgument','array','',true);
   }

public function render() 
    {
        return self::evaluateCondition($this->arguments) ? $this->renderThenChild() : $this->renderElseChild();
    }

/**
 * @return bool
 */
protected static function evaluateCondition($arguments = null) 
    {
         //do your stuff
         return true;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34729014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档