Twig

Extends \phpDocumentor\Transformer\Writer\WriterAbstract Implements \phpDocumentor\Transformer\Writer\Routable

A specialized writer which uses the Twig templating engine to convert templates to HTML output.

This writer support the Query attribute of a Transformation to generate multiple templates in one transformation.

The Query attribute supports a simplified version of Twig queries and will use each individual result as the 'node' global variable in the Twig template.

Example:

Suppose a Query indexes.classes is given then this writer will be invoked as many times as there are classes in the project and the 'node' global variable in twig will be filled with each individual class entry.

When using the Query attribute in the transformation it is important to use a variable in the Artifact attribute as well (otherwise the same file would be overwritten several times).

A simple example transformation line could be:

```
<transformation
    writer="twig"
    source="templates/twig/index.twig"
    artifact="index.html"/>
```

This example transformation would use this writer to transform the
index.twig template file in the twig template folder into index.html at
the destination location.
Since no Query is provided the 'node' global variable will contain
the Project Descriptor of the Object Graph.

A complex example transformation line could be:

```
<transformation
    query="indexes.classes"
    writer="twig"
    source="templates/twig/class.twig"
    artifact="{{name}}.html"/>
```

This example transformation would use this writer to transform the
class.twig template file in the twig template folder into a file with
the 'name' property for an individual class inside the Object Graph.
Since a Query *is* provided will the 'node' global variable contain a
specific instance of a class applicable to the current iteration.
see

for more information about variables in the Artifact attribute.

package

Default

Methods

Tries to add any custom extensions that have been defined in the template or the transformation's configuration.

addExtensionsFromTemplateConfiguration(\phpDocumentor\Transformer\Transformation $transformation, \phpDocumentor\Descriptor\ProjectDescriptor $project, \Twig_Environment $twigEnvironment) : void

This method will read the twig-extension parameter of the transformation (which inherits the template's parameter set) and try to add those extensions to the environment.

Throws
\InvalidArgumentException

if a twig-extension should be loaded but it could not be found.

Arguments

$twigEnvironment

\Twig_Environment

Adds the phpDocumentor base extension to the Twig Environment.

addPhpDocumentorExtension(\phpDocumentor\Descriptor\ProjectDescriptor $project, \phpDocumentor\Transformer\Transformation $transformation, string $destination, \Twig_Environment $twigEnvironment) : void

Arguments

$destination

string

$twigEnvironment

\Twig_Environment

Checks if there is a space in the path.

checkForSpacesInPath(string $path) : void
inherited
Throws
\InvalidArgumentException

if path contains a space.

Arguments

$path

string

This method verifies whether PHP has all requirements needed to run this writer.

checkRequirements() : void
inherited

If one of the requirements is missing for this Writer then an exception of type RequirementMissing should be thrown; this indicates to the calling process that this writer will not function.

Throws
\phpDocumentor\Transformer\Writer\Exception\RequirementMissing

when a requirements is missing stating which one.

Uses the currently selected node and transformation to assemble the destination path for the file.

getDestinationPath(\phpDocumentor\Descriptor\DescriptorAbstract $node, \phpDocumentor\Transformer\Transformation $transformation) : string|false

The Twig writer accepts the use of a Query to be able to generate output for multiple objects using the same template.

The given node is the result of such a query, or if no query given the selected element, and the transformation contains the destination file.

Since it is important to be able to generate a unique name per element can the user provide a template variable in the name of the file. Such a template variable always resides between double braces and tries to take the node value of a given query string.

Example:

An artifact stating classes/{{name}}.html will try to find the node 'name' as a child of the given $node and use that value instead.

Throws
\InvalidArgumentException

if no artifact is provided and no routing rule matches.

\UnexpectedValueException

if the provided node does not contain anything.

Arguments

Response

string|false

returns the destination location or false if generation should be aborted.

Returns the path belonging to the template.

getTemplatePath(\phpDocumentor\Transformer\Transformation $transformation) : string

Arguments

Response

string

Initializes the Twig environment with the template, base extension and additionally defined extensions.

initializeEnvironment(\phpDocumentor\Descriptor\ProjectDescriptor $project, \phpDocumentor\Transformer\Transformation $transformation, string $destination) : \Twig_Environment

Arguments

$destination

string

Response

\Twig_Environment

Sets the routers that can be used to determine the path of links.

setRouters(\phpDocumentor\Transformer\Router\Queue $routers) : void

setTranslator

setTranslator(\phpDocumentor\Translator\Translator $translator) 

Arguments

Abstract definition of the transformation method.

transform(\phpDocumentor\Descriptor\ProjectDescriptor $project, \phpDocumentor\Transformer\Transformation $transformation) : void
inherited abstract

Arguments

$project

\phpDocumentor\Descriptor\ProjectDescriptor

Document containing the structure.

$transformation

\phpDocumentor\Transformer\Transformation

Transformation to execute.

Properties

routers

routers : \phpDocumentor\Transformer\Router\Queue

translator

translator : \phpDocumentor\Translator\Translator