PHP code example of memio / twig-template-engine

1. Go to this page and download the library: Download memio/twig-template-engine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

memio / twig-template-engine example snippets




emio\Model\File;
use Memio\Model\Objekt;
use Memio\Model\Property;
use Memio\Model\Method;
use Memio\Model\Argument;

// Initialize the code generator
$loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/templates');
$twig = new \Twig\Environment($loader);

$line = new Memio\TwigTemplateEngine\TwigExtension\Line\Line();
$line->add(new Memio\TwigTemplateEngine\TwigExtension\Line\ContractLineStrategy());
$line->add(new Memio\TwigTemplateEngine\TwigExtension\Line\FileLineStrategy());
$line->add(new Memio\TwigTemplateEngine\TwigExtension\Line\MethodPhpdocLineStrategy());
$line->add(new Memio\TwigTemplateEngine\TwigExtension\Line\ObjectLineStrategy());
$line->add(new Memio\TwigTemplateEngine\TwigExtension\Line\StructurePhpdocLineStrategy());

$twig->addExtension(new Memio\TwigTemplateEngine\TwigExtension\Type());
$twig->addExtension(new Memio\TwigTemplateEngine\TwigExtension\Whitespace($line));

$templateEngine = new Memio\TwigTemplateEngine\TwigTemplateEngine($twig);
$prettyPrinter = new Memio\PrettyPrinter\PrettyPrinter($templateEngine);

// Describe the code you want to generate using "Models"
$myService = (new File('src/Vendor/Project/MyService.php'))
    ->setStructure(
        (new Objekt('Vendor\Project\MyService'))
            ->addProperty(new Property('createdAt'))
            ->addProperty(new Property('filename'))
            ->addMethod(
                (new Method('__construct'))
                    ->addArgument(new Argument('DateTime', 'createdAt'))
                    ->addArgument(new Argument('string', 'filename'))
            )
    )
;

// Generate the code and display in the console
echo $prettyPrinter->generateCode($myService);

// Or display it in a browser
// echo '<pre>'.htmlspecialchars($prettyPrinter->generateCode($myService)).'</pre>';



namespace Vendor\Project;

class MyService
{
    private $createdAt;
    private $filename;

    public function __construct(DateTime $createdAt, string $filename)
    {
    }
}