- How do I install spatie/html-element in a Laravel project?
- Run `composer require spatie/html-element` in your project directory. No Laravel-specific setup is required—it’s a framework-agnostic package. For better readability, wrap `HtmlElement::render()` in a helper function (e.g., `el()`) in your `AppServiceProvider` or a dedicated helper file.
- Can I use this package with Laravel Blade templates?
- Yes. Use `@php echo el('div', [...]) @endphp` to embed dynamic HTML in Blade files. The package works alongside Blade without conflicts, making it ideal for dynamic sections like email templates or admin panels where Blade’s static syntax falls short.
- Does spatie/html-element support Laravel’s HTML escaping (e.g., the `e()` helper)?
- No, by default it renders raw HTML. To prevent XSS, wrap user-provided content with Laravel’s `e()` helper or create a custom `safeEl()` function that escapes attributes/children. Example: `el('div', ['class' => e($userInput)])`.
- What Laravel versions does this package support?
- The package is PHP 8.0+ compatible and works with any Laravel 8.x/9.x/10.x version. It has no Laravel-specific dependencies, so it integrates seamlessly with modern Laravel apps. Tested against PHPUnit 9.x for compatibility.
- How do I handle nested HTML structures with this package?
- Use Emmet-style selectors for nesting (e.g., `el('div.container > ul.list > li.item')`) or pass elements as children. Example: `el('div', el('h1', 'Title'), el('p', 'Content'))`. The output mirrors your nested structure cleanly.
- Is spatie/html-element suitable for generating email templates in Laravel?
- Absolutely. Replace Markdown or static Blade views with dynamic `el()` calls for emails. Example: `el('table', ['class' => 'email-table'], el('tr', el('td', 'Hello')))` generates structured HTML for Mailables without string concatenation.
- What are the performance implications of deeply nested elements?
- For moderate complexity (e.g., 100+ nested elements), performance is negligible. However, deeply nested structures may impact memory. Benchmark with your expected use case—most Laravel apps won’t hit limits unless generating thousands of elements per request.
- Are there alternatives to spatie/html-element for Laravel HTML generation?
- For Laravel-specific needs, consider `laravelcollective/html` (deprecated) or Livewire’s built-in HTML rendering. For framework-agnostic solutions, `spatie/html-element` stands out for its Hyperscript/Emmet syntax. If you need Blade-like logic, stick with Blade’s `@if` directives.
- How do I test HTML output generated by this package?
- Use PHPUnit assertions like `assertStringContainsString()` or custom helpers to compare rendered HTML. For visual regression testing, tools like Laravel Dusk or Percy.io can validate dynamic output. Test edge cases like escaped attributes and nested structures.
- Is this package actively maintained? The last release was in 2022.
- While the last release was in 2022, Spatie maintains many packages long-term (e.g., `laravel-permission`). Check their [GitHub](https://github.com/spatie/html-element) for updates or community forks. For critical projects, consider wrapping the package in a custom class to isolate changes.