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.
twig/laravel or standalone). If the stack relies on Blade, integration requires evaluating trade-offs between Twig’s templating capabilities and Blade’s native Laravel integration. For projects adopting Inertia.js or Symfony-like templating, this package aligns well with dynamic HTML generation needs.twig/laravel (~1.5k stars).data_uri, html_classes, html_cva) that are pure Twig constructs, requiring no backend changes.html_cva objects are immutable, ensuring predictable CSS output.data_uri filter must be validated to prevent XSS (e.g., reject javascript: schemes). Laravel’s request validation can complement this.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Twig vs. Blade Conflict | High | Assess whether Twig is a hard requirement or if Blade’s @class directives suffice. If hybrid use is needed, test Twig’s namespace isolation (e.g., resources/views/twig/). |
| CSS Framework Overlap | Medium | Evaluate compatibility with Tailwind, Bootstrap, or custom CSS. html_cva may duplicate functionality if these frameworks already handle variants. |
| Data URI Performance | Low | Benchmark payload size increases for large assets. Cache data_uri outputs if used frequently. |
| Learning Curve | Medium | Provide internal documentation and pair programming for devs unfamiliar with Twig syntax. |
| Laravel Caching | Low | Ensure Twig template caching aligns with Laravel’s view:clear and config('view.cache'). |
html_cva integrate with existing CSS frameworks (e.g., Tailwind’s @apply or Bootstrap’s utility classes)?html_classes) or data_uri filters degrade render times for complex templates?data_uri to prevent XSS (e.g., rejecting javascript: or file: schemes)?@class directives or custom Blade components may suffice. However, Twig’s html_cva offers more advanced CSS variant management.data_uri, frontend libraries (e.g., data-uri-to-blob) could replace the Twig filter but add client-side complexity.html_cva could be preempted by Sass/PostCSS mixins, but Twig provides runtime flexibility.@twig directives or configure facades for shared logic.html_cva against current solutions (e.g., Tailwind, manual CSS classes) for template readability and maintainability.composer require twig/laravel twig/html-extra
config/twig.php:
'extensions' => [
Twig\Extra\Html\HtmlExtension::class,
],
html_cva and measure:
class="..." strings).AppServiceProvider:
public function boot()
{
$this->app['twig']->addExtension(new \Twig\Extra\Html\HtmlExtension());
}
use Twig\Extra\Html\HtmlExtension;
Twig::macro('html', function () {
return new HtmlExtension();
});
resources/views/twig/) to avoid conflicts.@twig('twig/_button.twig', ['text' => 'Save', 'primary' => true])
data_uri may bypass Laravel Mix. Ensure base64-encoded assets are cached and CDN-friendly.composer.json and configure it for a single template.html_classes/html_cva in high-impact components (e.g., buttons, cards).data_uri for embedded assets (e.g., icons, fallbacks) in performance-critical paths.{{ 'btn'|html_classes({'primary': isPrimary}) }}).html_cva enforces consistent variant naming (e.g., prevents btn-primary vs. btn_primary).data_uri supports modern web standards.twig:lint, custom error handlers).How can I help you explore Laravel packages today?