astroshippers / docx-template
Make new .docx files from a Handlebars-like template (variables, conditionals, loops, table-row repetition).
Fund package maintenance!
Requires
- php: ^8.5.0
Requires (Dev)
- laravel/pao: ^1.1
- laravel/pint: ^1.29.0
- pestphp/pest: ^5.0.0
- pestphp/pest-plugin-type-coverage: ^5.0.0
- phpstan/phpstan: ^2.1.49
- rector/rector: ^2.4.2
- symfony/var-dumper: ^8.0.8
README
Make new .docx files from a Handlebars-like template.
Write a Word document with {{placeholders}}, {{#if}}, {{#unless}}, and {{#each}} blocks — then render it with a map of assigns to produce a fresh .docx. Word frequently splits a single placeholder across multiple <w:r> runs (fonts, spellcheck markers, tracked changes); DocxTemplate heals those before substitution so your templates just work.
Requires PHP 8.5+
Installation
composer require astroshippers/docx-template
Usage
use DocxTemplate\Template; $bytes = Template::load('invoice.docx')->render([ 'customer' => 'Acme Corp', 'items' => [ ['name' => 'Widget', 'qty' => 3], ['name' => 'Gadget', 'qty' => 1], ], 'paid' => true, ]); file_put_contents('invoice-acme.docx', $bytes);
Template syntax inside the .docx:
Hello {{customer}}!
{{#each items}}
- {{name}} × {{qty}}
{{/each}}
{{#if paid}}Thanks for your payment.{{/if}}
{{#unless paid}}Please remit within 30 days.{{/unless}}
{{#each}} wrapping a single <w:tr> repeats the table row.
{{image var}} standalone in a paragraph embeds an image. Pass it as ['bytes' => binary, 'format' => 'png', 'width_cm' => 5, 'height_cm' => 3] in assigns.
Inspect a template's variables without rendering:
$vars = Template::load('invoice.docx')->variables(); // => ['customer', 'items', 'name', 'paid', 'qty']
Development
composer test # Run the full test suite composer lint # Run Pint linter composer refactor # Run Rector dry-run
License
MIT