- How do I install becklyn/html-builder in a Laravel project?
- Run `composer require becklyn/html-builder` in your project directory. The package has no Laravel-specific dependencies, so it integrates seamlessly with Blade, Livewire, or API responses. No additional configuration is required for basic usage.
- Does this package support Laravel 10+ and PHP 8.2+?
- While Symfony 6 support suggests PHP 8.1+ compatibility, there’s no explicit Laravel 10+ testing. Check for deprecated functions like `str_contains` or `array_merge` in your codebase. Test with PHP 8.2+ to confirm stability, especially if using Blade directives.
- Can I use this for dynamic HTML in emails or API responses?
- Yes, this package is ideal for generating HTML fragments dynamically. Use `HtmlElement` and `HtmlBuilder` to construct emails or API payloads, then render with `buildElement()`. For trusted raw HTML, inject `SafeMarkup` to bypass escaping.
- How do I integrate this with Laravel Blade?
- Create a Blade directive or service provider to wrap `HtmlBuilder` methods. For example, register a directive like `{{ html('div', ['class' => 'alert']) }}` to generate elements directly in Blade templates. No native Blade integration exists, so manual abstraction is required.
- What happens if I pass `false`, `null`, or `true` as attributes?
- The package handles these values automatically: `false` or `null` omits the attribute entirely, while `true` renders it as a boolean (e.g., `checked` becomes `<input checked>`). This is useful for conditional attributes like `disabled` or `readonly`.
- Is there a risk of XSS if I use this package?
- By default, all content is escaped to prevent XSS. For trusted HTML (e.g., user-uploaded content sanitized elsewhere), use `SafeMarkup` to inject raw HTML. Avoid mixing unsafe content directly into `HtmlElement` nodes.
- Are there alternatives like spatie/html or illuminate/html?
- Yes, `spatie/html` is a more maintained Laravel-native option with additional features like validation and CSS management. This package is lighter but lacks Laravel-specific tools. Choose based on needs: use `becklyn/html-builder` for simplicity, `spatie/html` for advanced features.
- How do I handle nested HTML elements or complex structures?
- Use `HtmlElement::addContent()` to nest elements or `SafeMarkup` for raw HTML. For example, add a `<div>` inside a `<section>` by chaining `addContent()` calls. The builder recursively processes nested nodes during rendering.
- Does Symfony 6 support affect Laravel compatibility?
- No direct impact, but Symfony 6 requires PHP 8.1+, which may indirectly improve PHP 8.2+ compatibility. The package remains framework-agnostic, so Laravel-specific features won’t change. Audit for dependency conflicts if using Symfony components alongside Laravel.
- What’s the best way to test this package in a Laravel app?
- Test with PHPUnit by mocking `HtmlBuilder` and verifying output against expected HTML strings. For Laravel 10+, check for deprecated function usage (e.g., `str_contains`) in your tests. Start with non-critical components like footers or alerts before full migration.