Create a custom Helper

Any Helper implemented in the frontend via JavaScript and used in Handlebars templates must also be replicated in PHP. For this purpose, the extension provides an interface Fr\Typo3Handlebars\Renderer\Helper\HelperInterface.


In the following examples, a Helper is created with the identifier greet. The associated class name is Vendor\Extension\Renderer\Helper\GreetHelper.

Implementation options

There are several ways to implement Helpers. To understand how Helpers are resolved in the Renderer, it is worth taking a look at the responsible Trait.

Basically every registered Helper must be callable. This means that both globally defined functions and invokable classes as well as class methods are possible. See what options are available in the following examples.

Global function

Any globally registered function can be used as a Helper, provided that it is recognized by the registered PHP autoloader.

function greet(array $context): string
    return sprintf('Hello, %s!', $context['hash']['name']);

Invokable class

Invokable classes can also be used as Helpers. For this it is necessary that they implement the method __invoke().

# Classes/Renderer/Helper/GreetHelper.php

namespace Vendor\Extension\Renderer\Helper;

use Fr\Typo3Handlebars\Renderer\Helper\HelperInterface;

class GreetHelper implements HelperInterface
    public function __invoke(array $context): string
        return sprintf('Hello, %s!', $context['hash']['name']);


Helpers can be registered either via configuration in the Services.yaml file or directly via the Renderer (if the default Renderer is used).

Manual registration

In addition to automatic registration, Helpers can also be registered manually at any time. For this purpose it is necessary to initialize the Renderer beforehand. Then a Helper can be registered with the registerHelper() method and thus made available in the Renderer:

    \Vendor\Extension\Renderer\Helper\GreetHelper::class . '::greetById'


See also

View the sources on GitHub: