apie/html-builders
HTML builder utilities for the Apie ecosystem. Provides internal components to generate and compose HTML fragments in PHP, used by other Apie packages. Developed in the Apie monorepo; limited standalone documentation available.
Installation:
composer require apie/html-builders
Ensure your composer.json meets the PHP 8.3+ requirement and includes apie/common and apie/core (auto-resolved via self.version).
First Use Case: Generate a simple HTML element (e.g., a button) in a Laravel Blade template or PHP file:
use Apie\HtmlBuilders\HtmlBuilder;
$button = HtmlBuilder::button()
->withText('Click Me')
->withClass('btn-primary')
->build();
echo $button; // Outputs: <button class="btn-primary">Click Me</button>
Key Entry Points:
HtmlBuilder facade/class: Central class for constructing HTML elements.withClass(), withId(), or withAttributes().build() to render the final HTML string.Blade Integration:
Register the facade in config/app.php under aliases:
'HtmlBuilders' => Apie\HtmlBuilders\Facades\HtmlBuilder::class,
Use in Blade templates:
@HtmlBuilders::div()
->withClass('container')
->withChildren(
HtmlBuilders::h1()->withText('Welcome')
)
->build()
Dynamic Element Generation: Loop through data to generate repeated HTML structures:
$items = ['Item 1', 'Item 2'];
$list = HtmlBuilder::ul()
->withChildren(
collect($items)->map(fn($item) =>
HtmlBuilder::li()->withText($item)
)
)
->build();
Form Handling: Build forms with validation-aware attributes:
$form = HtmlBuilder::form()
->withAction('/submit')
->withMethod('POST')
->withChildren(
HtmlBuilder::input()
->withName('email')
->withType('email')
->withClass('form-control')
)
->build();
Component-Based Patterns: Create reusable HTML components as classes:
class CardBuilder {
public static function build(array $data) {
return HtmlBuilder::div()
->withClass('card')
->withChildren(
HtmlBuilder::h3()->withText($data['title']),
HtmlBuilder::p()->withText($data['content'])
);
}
}
Integration with Laravel Collectives:
Combine with HTML facade for hybrid approaches:
use Collective\Html\Html as CollectiveHtml;
$element = HtmlBuilder::div()
->withChildren(
CollectiveHtml::link('Home', '/')
)
->build();
Monorepo Dependency:
apie-lib-monorepo). Ensure all apie/* packages are updated in sync to avoid version mismatches.self.version in composer.json or pin versions explicitly.Lack of Official Docs:
php artisan tinker):
\Apie\HtmlBuilders\HtmlBuilder::class->getMethods();
PHP 8.3+ Requirement:
php -v to verify compatibility before upgrading.Attribute Handling:
data-*) require explicit chaining:
HtmlBuilder::div()->withAttribute('data-custom', 'value');
withAttributes(['data-*' => [...]]) is unsupported).Child Elements:
withChildren() for nested elements, but ensure proper closure syntax:
// Correct:
HtmlBuilder::div()->withChildren(fn($div) =>
$div->withText('Nested')
);
HtmlBuilder instances may break rendering.UUID Integration:
ramsey/uuid. If generating UUIDs for IDs:
use Ramsey\Uuid\Uuid;
HtmlBuilder::div()->withId(Uuid::uuid4()->toString());
Inspect Built HTML:
Use dd() or dump() to debug intermediate states:
$element = HtmlBuilder::div()->withClass('debug');
dd($element->build()); // Inspect raw HTML
Check for Deprecated Methods: The package may evolve rapidly. Monitor the monorepo issues for breaking changes.
Fallback to Raw HTML:
For unsupported elements, fall back to Laravel’s e() or Html::entityEncode():
{!! Html::entityEncode('<div>Raw HTML</div>') !!}
Custom Builders:
Extend HtmlBuilder for domain-specific elements:
class ApiButtonBuilder extends HtmlBuilder {
public function withApiEndpoint(string $endpoint) {
return $this->withAttribute('data-endpoint', $endpoint);
}
}
Service Provider: Bind custom builders in a service provider:
$this->app->bind('apie.button', function() {
return new ApiButtonBuilder();
});
Testing:
Use phpunit to test builders in isolation:
public function testButtonBuilder() {
$button = HtmlBuilder::button()->withText('Test')->build();
$this->assertStringContainsString('button', $button);
}
Laravel Mix/Webpack: Process HTML strings with PostCSS or other tools if needed:
// webpack.mix.js
mix.postCss('resources/css/app.css', 'public/css', [
require('postcss-html')()
]);
How can I help you explore Laravel packages today?