Skip to content
Mario edited this page Oct 7, 2020 · 1 revision

Custom Renderer

There are several ways to implement to alter the output of a renderer. All of these methods can't be describes, but read the recommendations below:

Create your own renderer.

Creating your own renderer will provide the most flexibility, but also the most complexity. The recommended way of creating your own renderer is by extending the Main Renderer, implementing the Sub Renderer Interface.

use jblond\Diff\Renderer\MainRenderer;
use jblond\Diff\Renderer\SubRendererInterface;

class myRenderer extends MainRenderer implements SubRendererInterface
{
    // ...
}

Interface methods.

Because SubRendererInterface is implemented, all the following interface's methods must be implemented.

Method Parameter Return Value Type Description
generateDiffHeader string Returns a string value which represents the start of the output. E.g. A table header.
generateDiffFooter string Returns a string value which represents the end of the output. E.g. A table footer.
generateBlockHeader changes: array string Returns a string value which represents the start of a block of text.
A Block contains consecutive lines of which all of them has changes or none of them have changes.
generateBlockFooter changes: array string Returns a string value which represents the end of a block of text.
A Block contains consecutive lines of which all of them has changes or none of them have changes.
generateLinesDelete changes: array string Returns a string value which represents the lines that where partially modified.
generateLinesEqual changes: array string Returns a string value which represents the lines that are equal between the two versions.
generateLinesInsert changes: array string Returns a string value which represents the lines that don't exists in the 1st version, but do exist in the 2nd version.
generateLinesReplace changes: array string Returns a string value which represents the lines that do exists in the 1st version, but don't exist in the 2nd version.
generateSkippedLines string Returns a string value which represents the collapsing of lines that are equal between the two versions.
render false |string Should return false when both versions of the texts are equal or else a string represention of the differences.

Note: Your free to add additional methods and variables to support the custom renderer. Just take care that if you do override the methods and variables of the MainRenderer class, it's functionality might break.

Extending an existing renderer.

When one of the included renderers contains most of the wanted functionality, it also can be extended by a custom class.

By extending such a renderer, the custom renderer will have the same functionality as the one which is extended. In this case, you can override the parent's methods and variables.

use jblond\Diff\Renderer\Html\SideBySide;

class myRenderer extends SideBySide
{
    // Override the method that generates a string value which represents a table header.
    public function generateDiffHeader(): string
    {
/*
        Content of the original method (SideBySide)

        return <<<HTML
<table class="Differences DifferencesSideBySide">
    <thead>
        <tr>
            <th colspan="2">{$this->options['title1']}</th>
            <th colspan="2">{$this->options['title2']}</th>
        </tr>
    </thead>
HTML;
*/
    
    // New content of the method
    return <<<HTML
<table class="Differences DifferencesSideBySide">
    <thead>
        <tr>
            <th colspan="4">This is output generated by the overridden method!</th>
        </tr>
    </thead>
HTML;
    }
}

Note: Your free to add additional methods and variables to support the custom renderer. Just take care that if you do override the methods and variables of the MainRenderer class, it's functionality might break.

Warning

It's a goal to keep the library backwards compatible with previous versions as much as possibly. However, it can't be guaranteed the visibility or finalization of the library's classes, method and variables will not change in the future. E.g. The visibility of a public or protected variable may change to private, which means it can't be overridden anymore.