Search by

cable8mm / stub-template

cable8mm

The package is not a template engine, but you can use it like one to create stub files when needed.

Package info

github.com/cable8mm/stub-template

pkg:composer/cable8mm/stub-template

Statistics

Installs: 732

Dependents: 1

Suggesters: 1

Stars: 0

Open Issues: 0

v1.0.2 2026-10-28 20:25 UTC

README

Coding Style Actions Run Tests Actions Latest Version on Packagist Packagist Dependency Version Total Downloads Packagist Stars

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.