Document Twig templates, validate their inputs early, and ship confident UI changes.
TwigDoc is a zero-config companion for Twig projects. It extracts @var
annotations, turns them into documentation, and lets you fail fast by validating template contexts during development,
CI, or custom tooling.
Write documentation where it matters
: Keep @block, @var, nullable hints, and array shapes beside the Twig markup they describe.
Generate shareable documentation
: Build HTML or Markdown portals from annotations with twigdoc generate, complete with search, folder trees, and
placeholder coverage tracking.
Validate template inputs early
: TwigDoc::validate() and the twigdoc validate command compare runtime data to the declared contract so CI or local
dev trips on missing fields before reviewers do.
composer require smnandre/twigdocThe CLI lives in vendor/bin/twigdoc; the PHP API is namespaced under TwigDoc\.
Turn annotations into HTML or Markdown docs for your team:
vendor/bin/twigdoc generate templates/ --output=docs --format=html --include-empty -vv
open docs/index.htmldocs/index.html– a single-page app with a folder tree, responsive layout, and Cmd/Ctrl + K search.docs/twigdoc_data.js– the structured dataset (window.TWIGDOC_DATA) powering the SPA.
docs/index.md– a master index with descriptions, variable counts, and links.- Per-template Markdown files mirroring the template tree (
docs/user/profile.md, etc.) so you can commit documentation next to source.
Fail fast when runtime context deviates from the documented contract:
use TwigDoc\TwigDoc;
TwigDoc::validate(__DIR__.'/templates/user/profile.twig', [
'username' => 'Ada Lovelace',
'bio' => null,
'products' => [],
]);vendor/bin/twigdoc validate templates/Inspect the parsed documentation for a single template during reviews or pairing:
vendor/bin/twigdoc show templates/user/profile.twig{#
# @block User profile
# @var string $username Display name
# @var ?string $bio Optional bio
# @var Product[] $products Purchased products
#}
<h1>{{ username }}</h1>| Directive | Example | Meaning |
|---|---|---|
@var <type> $name [description] |
@var string $title Page title |
Declares a documented variable using PHP-style types. |
| Nullable types | @var ?string $bio |
Allows null or omission; non-nullables must exist in runtime context. |
| Arrays | @var Item[] $items |
Expects an array where every entry matches the inner type. |
| Scalars | string, int, float, bool (aliases integer, double, boolean). |
Primitives follow PHP naming. |
@block ... |
@block Product detail page |
Adds human-friendly copy for docs and search indices. |
| Descriptions | @var int $age User age in years |
Optional free text surfaced in HTML/Markdown outputs. |
Anything without annotations is ignored unless you pass --include-empty, which creates placeholder docs for coverage
tracking.
Contributions, bug reports, and RFCs are welcome! Please open issues or pull requests.
TwigDoc is released under the MIT Licence. See LICENSE for details.