Extending an existing renderer #687
-
Hi,
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Most renderers have only one method - final class MyImageRenderer implements InlineRendererInterface, ConfigurationAwareInterface
{
private ImageRenderer $innerRenderer;
public function __construct(ImageRenderer $imageRenderer)
{
$this->innerRenderer = $imageRenderer;
}
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
{
if (!($inline instanceof Image)) {
throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
}
// Have the core renderer generate the necessary `HtmlElement` or `string`
$output = $this->innerRenderer->render($inline, $htmlRenderer);
// TODO: Modify those $output contents here
return $output;
}
} Otherwise, if you do end up copy-pasting and duplicating some code, that's probably fine too. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Most renderers have only one method -
render()
. If you were to extend a renderer (likeMyImageRenderer extends ImageRenderer
) you wouldn't be able to change the inside of the parentrender()
method. You should instead consider implementing the decorator design pattern: