Custom rendering components
All components are described using interfaces. This makes it easy to exchange individual components. The following illustrates how such a use case can look.
Custom Renderer
The CPSIT\ interface describes a
Renderer. A distinction must be made as to whether the custom Renderer
is to be used for all components or only for individual variants.
Important
If you want your custom Renderer to be autoconfigured with all globally
registered Helpers, make sure to tag it with handlebars. and
implement the interface CPSIT\.
Global replacement
If the custom Renderer is to be used equally for all components, it can
simply be registered as a global replacement for the default Renderer in
the Services. file.
# Configuration/Services.yaml
services:
CPSIT\Typo3Handlebars\Renderer\Renderer:
alias: 'Vendor\Extension\Renderer\AlternativeRenderer'
Warning
Provide necessary dependencies
Note that if you use your own Renderer, you are responsible for providing
it with the necessary dependencies. These include the cache, Template,
and the default data (if needed). This is already configured with the default
Renderer.
Single replacement
A custom Renderer can also be used only for specific modules. In this case,
it replaces the default Renderer for the concrete Presenters.
Warning
Use of the Abstract required
The following example is only applicable to Presenters that extend the
Abstract, since it holds the required dependency in its constructor.
This is not part of the Presenter interface.
# Configuration/Services.yaml
services:
Vendor\Extension\Presenter\MyCustomPresenter:
arguments:
$renderer: ['@Vendor\Extension\Renderer\AlternativeRenderer']
Custom TemplateResolver
A standard Template exists for resolving template paths for templates
and partials. This is used in the default Renderer, but a custom
Template can also be used for specific purposes.
To use a custom Template, a corresponding class is created that
implements the CPSIT\
interface:
# Classes/Renderer/Template/AlternativeTemplateResolver.php
namespace Vendor\Extension\Renderer\Template;
use CPSIT\Typo3Handlebars\Renderer\Template\TemplateResolver;
class AlternativeTemplateResolver implements TemplateResolver
{
/**
* @var list<string>
*/
private array $supportedFileExtensions = ['hbs', 'hbs.html'];
public function getSupportedFileExtensions(): array
{
return $this->supportedFileExtensions;
}
public function supports(string $fileExtension): bool
{
return in_array(strtolower($fileExtension), $this->supportedFileExtensions, true);
}
public function resolveTemplatePath(string $templatePath): string
{
// ...
}
}
This is then used in the Services. file instead of the standard
Template:
# Configuration/Services.yaml
services:
CPSIT\Typo3Handlebars\Renderer\Template\TemplateResolver:
alias: 'Vendor\Extension\Renderer\Template\AlternativeTemplateResolver'