Skip to content
A generator for php based on Twig template engine
Find file
Pull request Compare This branch is 1 commit ahead, 43 commits behind cedriclombardot:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

TwigGenerator project status

TwigGenerator is a PHP code generator based on the Twig template engine. It leverages the power of Twig templates to simplify the generation of PHP code, to make it more extensible, and more readable.


Checkout this GitHub repository and the two submodules (Twig and Symfony ClassLoader):

git clone
cd TwigGenerator
git submodule update --init --recursive


To generate PHP classes, you need to create a "Builder", and one or more Twig templates. Then, add the new Builder to a "Generator", and generate the result.

Creating a Builder class

First, create a class extending TwigGenerator\Builder\BaseBuilder - no need for methods at start.


namespace MyProject\Builder;

use TwigGenerator\Builder\BaseBuilder;

class MyBuilder extends BaseBuilder

Tip: Alternatively, a builder can implement the TwigGenerator\Builder\BuilderInterface if it has to extend a custom class.

Creating Twig Templates

Next, create a couple twig templates under the templates/ directory. Usually, you need at least one template for the main structure, plus one template per feature added to the class.

Here is an example main template (or layout) for creating a custom PHP class (to be stored in templates/_base/common.php.twig):

{{ namespace is defined ? "namespace " ~ namespace ~ ";" : "" }}

class {{ className }} {{ extends is defined ? "extends " ~ extends : "" }}
{% block functions %}
{% endblock %}

And now, an example for adding a custom method (to be stored in templates/MyBuilder.php.twig):

{% extends "_base/common.php.twig" %}

{% block functions %}
    public function tellMeHello()
        echo "Hello world";
{% endblock %}

Generating the code

Use a TwigGenerator\Builder\Generator instance to generate the result. For instance:

// initialize the autoload
require_once '/path/to/TwigGenerator/autoload.php';
// alternatively, use your favorite PSR-0 autoloader configured with TwigGenerator, Symfony and Twig

// create a generator
$generator = new TwigGenerator\Builder\Generator();

// allways regenerate classes even if they exist -> no cache

// initialize a builder
$builder = new MyProject\Builder\MyBuilder();

// add the builder to the generator

// You can add other builders here

// add specific configuration for my builder
$builder->setVariable('className', 'MyBuilder');

// set common variables
    'namespace' => 'MyProject\Generated',

// Run generation for all builders

The file will be generated in MyProject\Generated\MyBuilder.php, as follows:

namespace MyProject\Generated;

class MyBuilder 
    public function tellMeHello()
        echo "Hello world";

Other Examples

You can see some basic code generation samples in the tests, and on some GitHub repositories like fzaninotto/Doctrine2ActiveRecord, or cedriclombardot/AdmingeneratorGeneratorBundle.

Something went wrong with that request. Please try again.