render()和renderStatic()有什么区别,以及在TYPO3中编写ViewHelper时应该使用什么? [英] What is the difference between render() and renderStatic() and what should be used when writing a ViewHelper in TYPO3
问题描述
有几个示例可以编写自定义ViewHelper以及不同的处理方式.我已经看到了render()和renderStatic的示例(例如,在开发自定义ViewHelper ).
There are several examples for writing a custom ViewHelper and different ways to do things. I have seen examples with render() and with renderStatic (for example in Developing a Custom ViewHelper).
在 24个流体提示中,有一个解释,但我没有不能理解它,也无法为我弄清楚为什么有2个功能以及在什么地方应使用什么功能.
In 24 Fluid Tips there is an explanation, but I don't understand it and it does not make it clear for me why there are 2 functions and what should be used where.
重大更改( Render方法参数on不赞成使用ViewHelpers ),并且各个地方的信息零散,很难在此处获得明确的答案.是开发自定义ViewHelper的示例最新的最佳做法?
Breaking changes (Render method arguments on ViewHelpers deprecated) and fragmented information in various places makes it difficult to get a clear answer here. Is the example in Developing a Custom ViewHelper up to date and best practice?
推荐答案
▸ renderStatic()
首先,renderStatic()
是一种静态 PHP方法.这意味着您无权访问实例属性,例如$this->tag
,例如,当ViewHelper类是AbstractTagBasedViewHelper
的子类时,这是必需的.
▸ renderStatic()
First of all, renderStatic()
is a static PHP method. This means, you do not have access to instance attributes, such as $this->tag
, which is for example required when your ViewHelper class is a subclass of AbstractTagBasedViewHelper
.
renderStatic()
的另一个缺点是您无法访问子节点.在以下示例中,子节点例如是<f:then>
:
Another drawback of renderStatic()
is that you can not access child nodes. A child node is for example <f:then>
in the following example:
<f:if condition="{variable}">
<f:then>
...
</f:then>
</f:if>
话虽如此,renderStatic()
具有最佳性能,因为它是从已编译的Fluid中调用的.因此,如果可能,应使用此方法.
Having said that, renderStatic()
features the best performance, because it is called from within compiled Fluid. Therefore, this method should be used if possible.
▸ render()
在某些情况下,此方法是更好的选择,或者必须用于特定的用例(请参见上面的说明). render()
方法中实现的逻辑未编译,这会影响缓存的输出.
▸ render()
Under certain circumstances, this method is the better choice or has to be used for a specific use case (see explanations above). The logic implemented in a render()
method is not compiled, which has an impact on output that is cached.
render()
的缺点是它的性能.
▸摘要/附加说明
- 如果可以(使用),请使用
renderStatic()
. - 如果实现TagBased-ViewHelper(
的子类,请使用render()
TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
). - 如果需要读取子节点,请使用
render()
. - 以Fluid(例如TYPO3 v9 LTS)随附的ViewHelper为例进行查看.
(请参阅:typo3/sysext/fluid/Classes/ViewHelpers/*
)[ GitHub ]
- Use
renderStatic()
, if you can (performance). - Use
render()
, if you implement an TagBased-ViewHelper (subclass of
TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
). - Use
render()
, if you need to read child nodes. - Review the ViewHelpers that are shipped with Fluid (e.g. TYPO3 v9 LTS) as examples.
(see:typo3/sysext/fluid/Classes/ViewHelpers/*
) [GitHub]
这篇关于render()和renderStatic()有什么区别,以及在TYPO3中编写ViewHelper时应该使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!