Repository navigation
Extending an existing renderer #687
|
Hi, |
Answered by
colinodell
Jul 4, 2021
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. |
0 replies
Answer selected by
colinodell
|
|
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: