This feature is still under development and only available in the nightly docker builds.
Twig extension
phpDocumentor uses twig for most rendering of the output. Twig gives us the flexibility we need and allows you to customize just what you need in a template. For more advanced use-cases twig supports extensions, these give you more options to write custom behavior that impacts the output of phpDocumentor.
read first how to setup an phpDocumentor extension before you continue this guide.
Now you have setup your phpDocumentor extension it is time to create a twig extension. In our example we will add
a custom filter <https://twig.symfony.com/doc/3.x/advanced.html#filters>
which can be used in a template to transform text.
We can do this by creating a class like this:
<?php
declare(strict_types=1);
namespace MyExtension\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
final class MyExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('rot13', function ($string) {
return str_rot13($string);
})
];
}
}
Register the twig extension
A twig extension needs to be registered in phpDocumentor before you can use it in your templates. To do so we need
to add a new definition in the service container. With the tag twig.extension
<?php
declare(strict_types=1);
namespace MyExtension\Twig;
use phpDocumentor\Extension\Extension as BaseExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
final class Extension extends BaseExtension
{
public function load(array $configs, ContainerBuilder $container)
{
$container->addDefinitions([
(new Definition(MyExtension::class))->addTag('twig.extension'),
]);
}
}
Using the filter
Once your phpDocumentor extension is ready you can start using it in your template. This works for complete custom templates as well for small overwrites.