2017 © Pedro Peláez
 

library html-builder

Build html tags with a fluent API

image

comsolit/html-builder

Build html tags with a fluent API

  • Thursday, May 21, 2015
  • by comsolit
  • Repository
  • 10 Watchers
  • 3 Stars
  • 1,643 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 3 % Grown

The README.md

THIS REPO IS UNMAINTAINED / A NEW MAINTAINER IS NEEDED

HTMLBuilder

Lightweight utility class for building small snippets of HTML in PHP., (*1)

Examples

All examples expect a use Comsolit\HTMLBuilder; statement., (*2)

$builder = new HTMLBuilder('a', 'comsolit AG');
$builder->addAttribute('href', 'https://www.comsolit.com');

$this->assertEquals(
    '<a href="https://www.comsolit.com">comsolit AG</a>',
    $builder->build()
);

Input tag without closing tag

$builder = new HTMLBuilder('input');
$builder
    ->setVoid() // don't build a closing tag
    ->addAttribute('type', 'text')
    ->addAttribute('name', 'some_name');

$this->assertEquals(
    '<input type="text" name="some_name"/>',
    $builder->build()
);

Tag with inner text

$builder = new HTMLBuilder('span', 'my text');

$this->assertEquals(
    '<span>my text</span>',
    $builder->build()
);

Using the magic __toString() method

$builder = new HTMLBuilder('span');

$this->assertEquals(
    $builder->build(),
    (string)$builder
);

Wrap tags

$inputBuilder = (new HTMLBuilder('input'))->setVoid();
$labelBuilder = new HTMLBuilder('label', $inputBuilder);

$this->assertEquals(
    '<label><input/></label>',
    (string)$labelBuilder
);

Encapsulate tags

$inputBuilder = (new HTMLBuilder('input'))->setVoid();
$labelBuilder = $inputBuilder->encapsulate('label');

$this->assertEquals(
    '<label><input/></label>',
    (string)$labelBuilder
);

Set attribute conditionally

$builder = (new HTMLBuilder('input'))
    ->setVoid()
    ->addAttribute('type', 'checkbox')
    ->addAttributeIf(1 + 1 === 3, 'disabled', 'disabled')
    ->addAttributeIf(1 + 1 === 2, 'checked', 'checked');

$this->assertEquals(
    '<input type="checkbox" checked="checked"/>',
    (string)$builder
);

Special method for classes

$builder = (new HTMLBuilder('span'))
    ->addClass('once')
    ->addClass('multiple')
    ->addClass('multiple');

$this->assertEquals(
    '<span class="once multiple"></span>',
    (string)$builder
);

Add multiple children

$builder = new HTMLBuilder('ul');

for ($i = 0; $i < 2; ++$i) {
  $builder->addChild(new HTMLBuilder('li', (string)$i));
}

$this->assertEquals(
    '

  • 0
  • 1
', (string)$builder );

We've used this builder already in three projects and than evaluated other packages to decide whether we want to publish this builder or not:, (*3)

The Versions

21/05 2015

dev-master

9999999-dev

Build html tags with a fluent API

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

21/05 2015

1.0

1.0.0.0

Build html tags with a fluent API

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires