phpunit/php-text-template
phpunit/php-text-template is a small PHP library for rendering text templates by substituting variables into template files or strings. Commonly used by PHPUnit and related tools to generate code, reports, and other text output from simple placeholders.
Install via Composer: composer require phpunit/php-text-template. This is a purely functional templating tool—no service provider, no config, no Laravel integration. Use the SebastianBergmann\Template\Template class directly to replace {placeholder} (escaped) or {!placeholder} (raw) markers. Start with a minimal example:
use SebastianBergmann\Template\Template;
$template = new Template('Welcome, {username}! Your balance is ${balance}.');
$output = $template->render([
'username' => 'Alex',
'balance' => '1,250.00'
]);
echo $output; // "Welcome, Alex! Your balance is $1,250.00."
First use case: Generate CLI output, config stubs, or test fixtures—e.g., scaffolding .env.example files or static API response samples for docs.
.tpl or .txt files and load via file_get_contents(). Ideal for pre-rendered artifacts (e.g., README snippets, migration stubs).
$template = new Template(file_get_contents('stubs/migration.tpl'));
file_put_contents('database/migrations/'.date('Y_m_d_His').'_create_users.php', $template->render(['table' => 'users']));
{!inner}—useful for building multi-layer scaffolds (e.g., a controller template embedding a model template).config/app.php matching a golden template).php artisan make:template user-invite → populates resources/emails/user-invite.txt.render() compiles on every call, pre-instantiate templates for high-frequency use:
$cachedTemplates = [];
function getTemplate(string $name): Template {
return $cachedTemplates[$name] ??= new Template(file_get_contents("templates/$name.tpl"));
}
@if, @foreach, or @include—all branching/looping must be done before calling render(). Prepare arrays of data and flatten nested structures manually.{foo} escapes &, <, >, ", '—but only ASCII. {!foo} is raw HTML injection risk if used with user content. Override escaping via Template::escape() for custom behavior (e.g., JSON-safe output).{ foo } will not match ['foo' => 'bar']. Use exact keys with no spaces.^4.0) if targeting newer PHP runtimes—this isn’t stable for broad adoption.file_get_contents failure), or NULL values (silently rendered as empty string). Log array_keys() against the template string to validate.TextTemplateRenderer service to handle defaults, validation, and error logging—this keeps Laravel’s lifecycle clean.How can I help you explore Laravel packages today?