Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Html Builder

Html Builder Laravel Package

becklyn/html-builder

Lightweight PHP HTML builder for composing elements and attribute strings. Create HtmlElement nodes, render with escaping by default, support boolean/null/false attribute rules, and inject trusted raw HTML via SafeMarkup when needed.

View on GitHub
Deep Wiki
Context7

Simple helpers for building HTML

Frequently asked questions about Html Builder
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai