PHP code example of lagdo / html-builder

1. Go to this page and download the library: Download lagdo/html-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lagdo / html-builder example snippets


$builder = new HtmlBuilder();

return $builder->build(
    $builder->div(
        $builder->html('Hello&nbsp;'),
        $builder->b(
            $builder->text('World')
        ),
        $builder->comment('Salutations')
    )
);

$builder = new HtmlBuilder();

return $builder->build(
    $builder->div($builder->text('Content'))
        ->setId('div-id')
        ->setClass('div-class')
        ->setDataKey('div-key')
);

return $builder->build(
    $builder->div($builder->text('Content'))
        ->when($important, fn($component) => $component->addClass('important'))
);

function formatParagraph($component)
{
    $component->setClass('paragraph');
}

return $builder->build(
    $builder->div($builder->text('Content'))
        ->with(formatParagraph(...))
);

return $builder->build(
    $builder->div(
        $builder->text('Welcome'),
        $builder->when($user !== null, fn() => $builder->html("&nbsp;{$user->name}"))
    )
);

return $builder->build(
    $builder->div(
        $builder->pick(
            $builder->when($user === null, fn() => $builder->text('Welcome')),
            $builder->when($user->isAdmin, fn() => $builder->html("Welcome&nbsp;<b>{$user->name}</b>")),
            $builder->when(true, fn() => $builder->html("Welcome&nbsp;{$user->name}"))
        )
    )
);

return $builder->build(
    $builder->ul(
        $builder->each($menuItems, fn($menuItem) => $builder->li($menuItem->title))
    )
);

return $builder->build(
    $builder->ul(
        $builder->loop(
            $menuItems,
            fn($menuItem, $loop) => $builder->li($menuItem->title)
                ->setClass($loop->cycle('item-odd', 'item-even'))
                ->when(
                    $loop->changed(fn($item) => $item?->group ?? ''),
                    fn($menuComponent) => $menuComponent->setClass('item-first')
                )
        )
    )
);

return $builder->build(
    $builder->ul(
        $builder->each($menuItems, fn($menuItem) =>
            $builder->list(
                $builder->li($menuItem->title),
                $builder->comment($menuItem->group)
            )
        )
    )
);

    public function registerBuilderHelper(string $prefix, Closure $helper): void;
    public function registerElementHelper(string $prefix, Closure $helper): void;
    public function registerComponentHelper(string $prefix, Closure $helper): void;

use Lagdo\HtmlBuilder\Element\Element;
use Lagdo\HtmlBuilder\HtmlComponent;
use Lagdo\HtmlBuilder\HtmlElement;
use LogicException

/**
  * @throws LogicException
  */
function builderHelper(string $tagName, string $method, array $arguments): Element
{
    // Create and return a component
}

/**
  * @throws LogicException
  */
function elementHelper(HtmlElement $element, string $tagName, string $method, array $arguments): HtmlElement
{
    // Customize and return the $element object.
}

/**
  * @throws LogicException
  */
function componentHelper(HtmlComponent $component, string $tagName, string $method, array $arguments): HtmlComponent
{
    // Customize and return the $component object.
}

$builder->registerBuilderHelper('jxn', builderHelper(...));