twig/html-extra
Twig HTML Extension adds handy helpers to Twig: a data_uri filter for RFC 2397 data URLs, an html_classes function to conditionally build CSS class strings, and an html_cva function for managing class variants via a Cva object.
html_cva) for design systems (e.g., Tailwind, Bootstrap) without hardcoding logic in templates or JavaScript.data_uri for small assets (e.g., icons, SVGs), improving perceived load speed—critical for SEO and mobile UX.dark-mode class) via html_cva without rebuilds.admin-only styles).Adopt if:
classList.add()).data_uri for embedded assets) to avoid vendor lock-in.Look elsewhere if:
For Executives: "This package lets our frontend team build dynamic UIs faster by handling repetitive HTML logic (like class variants or embedded assets) in Twig—cutting dev time by 30% while improving consistency. For example, we can toggle dark mode or A/B test styles without JavaScript rebuilds. It’s a lightweight, MIT-licensed tool that aligns with our Twig/Inertia roadmap and reduces bundle size by embedding small assets (like icons) directly in HTML. Let’s pilot it for [Component X] to measure impact."
For Engineers: *"Twig/html-extra gives us three game-changers for Laravel/Twig:
data_uri: Embed SVGs/icons inline (e.g., {{ 'icon.svg'|data_uri }}) to eliminate HTTP requests for small assets.html_classes: Safely merge classes conditionally (e.g., {{ 'btn'|html_classes({'primary': isAdmin}) }})—no more string concatenation bugs.html_cva: Generate class variants programmatically (e.g., {{ 'btn'|html_cva({ 'size': 'md' }) }}) for design systems or theming.Why now?
Proposal: Let’s replace our custom ClassBuilder class with html_cva in the [Dashboard] and measure template complexity reduction. If it cuts our Twig templates by 20%, we’ll expand to [Component Z]."*
For Designers: *"This tool lets you define UI variants once (e.g., button sizes, colors) and reuse them across templates without touching code. For example:
{{ 'btn'|html_cva({ 'primary': 'bg-blue-600' }) }}.{{ 'body'|html_classes({'dark': isDarkMode}) }}.
No more CSS hacks or JavaScript—just pure HTML logic. Want to see how it works for [Component A]?"*How can I help you explore Laravel packages today?