cable8mm / stub-template
The package is not a template engine, but you can use it like one to create stub files when needed.
Requires
- php: ^8.1
- twig/twig: ^3.0
Requires (Dev)
- laravel/pint: ^1.20
- phpunit/phpunit: ^9.0|^10.0|^11.0
Suggests
- laravel/pint: Provide a easy way to fit coding style
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-28 20:26:52 UTC
README
Generate PHP source files from Twig-powered stubs without executing the PHP code inside them. Use it to fill in values such as namespaces and class names while keeping the generated PHP code intact.
It needs to be used with Twig template syntax.
Installation
You can install the package via composer:
composer require cable8mm/stub-template
Usage
$rendered = Stub::of(__DIR__.'/stubs/model.stub', [ 'namespace' => 'App\\Models', 'class_name' => 'Product', ] )->render();
or
$rendered = Stub::of('stubs/model.stub', [ 'namespace' => 'App\\Models', 'class_name' => 'Product', ], __DIR__ )->render();
stubs/model.stub:
<?php namespace {{ namespace }}; final class {{ class_name }} { public function __construct( private string $name, ) {} }
The result is a PHP source string:
<?php namespace App\Models; final class Product { public function __construct( private string $name, ) {} }
Twig renders the template syntax, but PHP code in the stub is output as text and is not executed.
Twig can HTML-escape interpolated values when HTML autoescaping is enabled. If
you are generating PHP or another source file, make sure the escaping mode
matches that output. You can use Twig's raw filter for trusted values that
must be inserted without escaping, or configure autoescaping for the template
as needed. Only use raw with values you trust, since it bypasses escaping.
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email cable8mm@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
PHP Package Boilerplate
This package was generated using the PHP Package Boilerplate by Beyond Code.