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 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

Getting Started

Minimal Setup

  1. Installation:

    composer require becklyn/html-builder
    

    No configuration required—just autoload via Composer.

  2. First Use Case: Generate a basic HTML string:

    use Becklyn\HtmlBuilder\Html;
    
    $html = Html::tag('div', 'Hello, World!');
    echo $html; // Outputs: <div>Hello, World!</div>
    
  3. Key Entry Points:

    • Html::tag() – Core method for creating HTML elements.
    • Html::attr() – Helper for attributes (e.g., Html::attr('class', 'btn')).
    • Html::raw() – Escape HTML safely (e.g., Html::raw('<b>bold</b>')).

Implementation Patterns

Common Workflows

  1. Dynamic Element Creation:

    $user = User::find(1);
    $avatar = Html::tag('img', '', [
        'src' => $user->avatar_url,
        'alt' => $user->name,
        'class' => 'avatar',
    ]);
    
  2. Nested Structures:

    $card = Html::tag('div', [
        Html::tag('h3', 'Title'),
        Html::tag('p', 'Content'),
    ], ['class' => 'card']);
    
  3. Reusable Components:

    function button(string $text, array $attrs = []): string {
        return Html::tag('button', $text, $attrs);
    }
    // Usage: button('Submit', ['class' => 'btn-primary'])
    
  4. Integration with Blade:

    @php
        $alert = Html::tag('div', 'Warning!', ['class' => 'alert']);
    @endphp
    {!! $alert !!}
    

Performance Tips

  • Cache Complex HTML: Store generated HTML in a variable or view data to avoid reprocessing.
  • Batch Attributes: Use Html::attr() for dynamic attributes (e.g., in loops):
    $items = collect([1, 2, 3]);
    $list = $items->map(fn($id) => Html::tag('li', '', [
        'data-id' => $id,
    ]))->implode('');
    

Gotchas and Tips

Pitfalls

  1. XSS Risks:

    • Issue: Html::tag() escapes content by default. Use Html::raw() only for trusted HTML.
    • Fix: Sanitize user input before passing to Html::raw() (e.g., with Str::of()->markdown()).
  2. Attribute Quoting:

    • Issue: Attributes with special characters (e.g., class="btn btn--primary") may break.
    • Fix: Use Html::attr() for dynamic attributes or manually escape:
      Html::tag('div', '', ['class' => 'btn btn--"primary"']);
      
  3. Self-Closing Tags:

    • Issue: <img /> vs <img> syntax may cause rendering issues.
    • Fix: Explicitly close tags or use Html::tag('img', '', [], true) (if supported).

Debugging

  • Inspect Output: Use dd($html) to verify generated markup.
  • Check for Whitespace: Html::tag() trims content by default. Use Html::raw() for pre-formatted strings.

Extension Points

  1. Custom Tag Helpers:

    Html::macro('link', function(string $text, string $url, array $attrs = []) {
        return Html::tag('a', $text, array_merge($attrs, ['href' => $url]));
    });
    // Usage: Html::link('Click', '/home')
    
  2. Override Default Behavior: Modify the Html facade binding in AppServiceProvider:

    Html::macro('customTag', function() { ... });
    
  3. Integration with Laravel Collectives: Combine with HTML facade for Blade compatibility:

    use Collective\Html\Html as CollectiveHtml;
    CollectiveHtml::macro('becklynTag', function($tag, $content = null) {
        return app(\Becklyn\HtmlBuilder\Html::class)->tag($tag, $content);
    });
    
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.
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon