astroshippers/docx-template

Make new .docx files from a Handlebars-like template (variables, conditionals, loops, table-row repetition).

Maintainers

Package info

github.com/astroshippers/docx-template

pkg:composer/astroshippers/docx-template

Fund package maintenance!

osbre

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.1 2026-06-08 12:29 UTC

This package is auto-updated.

Last update: 2026-06-08 12:34:39 UTC


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