phpunit/php-text-template
A tiny, dependency-free templating library used internally by PHPUnit. Render text with {placeholder} syntax using an associative array of values. Battle-tested and simple, it suits report generation, log formatting, and other human-readable output where a full template engine would be overkill.
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.
Version note: Use 6.0.1+ if replacement values may contain placeholder-like text. It fixes render() re-processing {...} or {!...} sequences that were inserted as values for earlier placeholders.
.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 = (new Template(file_get_contents('stubs/model.tpl')))->render(['model' => 'User']);
$outer = new Template(file_get_contents('stubs/controller.tpl'));
$controller = $outer->render(['model' => 'User', 'model_stub' => $inner]);
If you need recursive substitution, do it explicitly with a second render() call rather than relying on the renderer to reprocess inserted values.config/app.php matching a golden template). The 6.0.1 fix is helpful when fixture data contains template syntax intentionally.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.{name} or {!html} could be replaced again if another placeholder with that key was processed. 6.0.1 stops that. This is a bug fix, but it changes output for templates that accidentally relied on recursive replacement; use explicit multi-pass rendering if that behavior is intentional.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.{!placeholder} output safe for user-supplied content.How can I help you explore Laravel packages today?