Using twig

phpDocumentor provides the Twig 1.x engine as a modern alternative to XSL templates.

Configuration

The Twig engine can be configured in your template’s template.xml through the <parameters> tag.

<template>
    <parameters>
        <parameter key="config-key">config-value</parameter>
    </parameters>
</template>

Caching

phpDocumentor enables Twig’s compilation cache by default. When developing Twig templates, you need to turn this off in your template.xml by enabling debug mode:

<parameter key="twig-debug">true</parameter>

This will also make the Twig debug extension available, which provides the dump() function.

Custom Extensions

Custom Twig extensions can be enabled and configured through parameters.

<parameter key="twig-extension">Your\Custom\Extension</parameter>

<!-- You may configure multiple extensions by repeating the parameter with different values -->
<parameter key="twig-extension">Your\Custom\SecondExtension</parameter>

phpDocumentor will attempt to autoload the class, allowing you to use Composer’s autoloading functionality to load the class.

Your extension class may optionally implement phpDocumentor\Transformer\Writer\Twig\ExtensionInterface, which will cause your constructor to be passed additional information about the project and transformation.

Global Variables

The Twig writer provides two primary variables for use in your templates:

  • project (phpDocumentor\Descriptor\ProjectDescriptor) - This variable contains the top-level project data generated by phpDocumentor.
  • node (subclass of phpDocumentor\Descriptor\DescriptorAbstract) - This variable contains the data from the transformation query. This will be one of the Descriptor classes matching the object you query.

Functions

path

{{ path(relative_path) }}
relative_path
type: string

Returns the URL for the given path, relative to the current path.

Filters

markdown

{{ text|markdown }}
text
type: string

Renders Markdown text into HTML, using the Parsedown engine.

trans

{{ message|trans(context = []) }}
message
type: string
context (optional)
type: array default: []

Renders the translation of the content. Variables in context are expanded into the translated text using vsprintf.

route

{{ node|route(presentation = 'normal') }}
node
type: phpDocumentor\Descriptor\DescriptorAbstract
presentation (optional)
type: string default: normal

Renders a link to the generated page for the node. presentation can be set to 'url' to render only the URL.

sort_asc

{{ list|sort_asc }}
list
type: array

Sorts the given list in ascending order by name, case-insensitively. Uses the name of each node in the collection to sort. Typically used for sorting a phpDocumentor\Descriptor\Collection of descriptors.

sort_desc

{{ list|sort_desc }}
list
type: array

Sorts the given list in descending order by name, case-insensitively. Uses the name of each node in the collection to sort. Typically used for sorting a phpDocumentor\Descriptor\Collection of descriptors.