lelyfoto/twig-instanceof
Twig extension that adds an instanceof test, letting templates check whether a value is an instance of a given class or interface. Lightweight and easy to register for cleaner, type-aware Twig conditionals.
instanceof expression/test in Twig. This package is a decision not to maintain that glue yourself. The real PM question is whether a small, low-adoption dependency is worth the vendor risk versus copying ~30–50 lines under MIT into your own Twig extension. v1.3 slightly improves the "adopt" side of that ledger without changing the calculus: the package now supports Symfony 8 and fixes a PHP/Twig deprecation warning, and it picked up two first-time outside contributors. That is evidence of maintenance, not of a support contract.rcrowe/twigbridge or a Symfony codebase. If your roadmap leans on Twig, small ergonomic gaps like this compound; if it's incidental, this package is noise.Adopt when all of the following hold:
{% if item.type == 'video' %} string checks or controller-side collection partitioning.instanceof is refactor-safe in a way that magic type strings are not.^1.3 rather than a looser constraint if that is on your roadmap. The return-type/deprecation fix also makes it usable on current PHP versions without warning noise.Look elsewhere (or just write the test yourself) when:
is defined, is null, is same as(), attribute(), or a semantic custom test like {% if block is renderable as 'video' %} — which is usually the better design because it decouples templates from class names.RenderableBlock interface with a render() method beats instanceof chains almost every time.To executives: A small, MIT-licensed Twig extension that lets templates distinguish between object types when rendering mixed content — think content blocks, activity feeds, or form fields. It removes a class of brittle string-comparison hacks from our templates and costs effectively nothing to try. The newest release (v1.3) is a maintenance release: it adds support for Symfony 8 and fixes a deprecation warning, and it drew two new outside contributors — modest but real evidence the package is being kept current, which matters for our upgrade path. The caveat stands: it is a tiny project with negligible adoption, so we should treat it as a pinned utility and be ready to vendor the (small) source if it goes stale. Low cost, low risk, modest upside.
To engineering: Single-purpose extension adding an instanceof expression to Twig, relevant only where we already run Twig (Symfony, or Laravel + TwigBridge). It buys us type-safe branching in templates instead of magic type strings. v1.3 is non-breaking: Symfony 8 support plus a return-type fix for a deprecation message, so upgrading from v1.2 is low-effort, and it removes friction if Symfony 8 is on our horizon. Two guardrails: (1) review and pin the source at ^1.3 — it is small enough to fork outright if we prefer; (2) prefer wrapping it in a semantic Twig test so templates don't hardcode PHP class names, and keep genuine business logic out of templates. If we only need it in a handful of places, writing the extension in-house is a legitimate alternative and avoids the dependency entirely.
How can I help you explore Laravel packages today?