简体   繁体   English

如何从拼写错误 (.ts) 文件调用 TYPO3 自定义 viewhelper 并从 viewhelper 接收响应?

[英]How to call a TYPO3 custom viewhelper from a typoscript (.ts) file and receive a response from viewhelper?

I tried to call a custom viewhelper in TYPO3 9.5.9 from a typoscript file.我试图从 typoscript 文件调用 TYPO3 9.5.9 中的自定义 viewhelper。 But it is not working.但它不起作用。 An example for viewhelper is given below:下面给出了 viewhelper 的示例:

<?php
namespace MyVendor\BlogExample\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

class GravatarViewHelper extends AbstractViewHelper
{
   use CompileWithRenderStatic;

   public static function renderStatic(
       array $arguments,
       \Closure $renderChildrenClosure,
       RenderingContextInterface $renderingContext
   ) {
       return 'World';
   }
}

How can I call this custom viewhelper from a typoscript (.ts) file and get a response?如何从打字错误 (.ts) 文件中调用此自定义 viewhelper 并获得响应?

I would suggest you extract the function that "does what the ViewHelper does" to a separate method and make it possible to call this method as a public method, and make it receive the PHP function arguments it requires (user email address, for example) *wrapped in an array $parameters .我建议您将“执行 ViewHelper 的操作”的 function 提取到一个单独的方法中,并使其可以将此方法作为公共方法调用,并使其接收所需的 PHP function arguments(例如,用户 email 地址) *包装在array $parameters中。

This method can then be called from the ViewHelper as well as from TypoScript.然后可以从 ViewHelper 和 TypoScript 调用此方法。 The TypoScript would be something like: TypoScript 类似于:

lib.myGravatarObject = USER
lib.myGravatarObject.userFunc = MyVendor\ExtensionName\GravatarRenderer
lib.myGravatarObject.userEmail = foo@bar.com

If you name the properties of the array $parameters the same as your ViewHelper arguments you can simply pass $this->arguments or $arguments from within the ViewHelper (the former if you are implementing render() , the latter if you are implementing renderStatic() ).如果您将array $parameters的属性命名为与您的 ViewHelper arguments 相同,您可以简单地从 ViewHelper 中传递$this->arguments$arguments (前者如果您正在实施render() ,后者如果您正在实施renderStatic() ). It also makes it more transparent that you use the same property names in TS as you would use for ViewHelper arguments.它还使您在 TS 中使用与用于 ViewHelper arguments 相同的属性名称变得更加透明。

You can use a FLUIDTEMPLATE cObject with a minimal template (eg via template property):您可以使用带有最小模板的 FLUIDTEMPLATE cObject(例如通过模板属性):

10 = FLUIDTEMPLATE
10 {
   template.cObject = TEXT
   template.cObject.value(
      <html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" xmlns:mv="http://typo3.org/ns/MyVendor/BlogExample/ViewHelpers" data-namespace-typo3-fluid="true">
      <mv:GravatarViewHelper />
   )
}

It seems to be possible.这似乎是可能的。 But as Bernd already asked: Why would one do that?但正如 Bernd 已经问过的:为什么要那样做?

Why should you do this?你为什么要这样做? (please describe your use case) (请描述您的用例)

Viewhelpers are for usage in FLUID templates, there you have the correct and matching context. Viewhelpers 用于 FLUID 模板,那里有正确且匹配的上下文。

If you want to call a PHP function inside of typoscript directly you have the option of an userfunc ( manual ).如果您想直接在 typoscript 中调用 PHP function,您可以选择userfunc手动)。 But you need to provide the necssary context.但是您需要提供必要的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM