An open API service indexing awesome lists of open source software.

https://github.com/nicklayb/htmlbuilder

PHP Helper for building HTML tags and stuff
https://github.com/nicklayb/htmlbuilder

Last synced: about 1 year ago
JSON representation

PHP Helper for building HTML tags and stuff

Awesome Lists containing this project

README

          

# HtmlBuilder
Useful helper for builder HTML entities with php

You should not use this to build full DOM et full pages since it mays get harder to load than pure HTML, however it ensure a correct HTML syntax and some friendly tools for creating tags.

A use case example would be outputting an edit button to a datatable.

## Introduction

Install it via composer
```
composer require nicklayb/htmlbuilder
```

### Creating simple DOM element

For example, if you would like to create a `input` of type `text` with name `my_input` and classes `super-input` and `holy-input`, you could do it that way.
```php
';
```
Or you could do it that way
```php
name('my_input')
->classes([
'super-input',
'holy-input',
])
$tag->make();

```

The make method will return your string dom element

You may say "Well, how is that an improvement if it takes more lines?". It simply because it allows you to manipulate element easily. You could swipe class in an easier way than concatenating string. And so, I found it's better to take 5 lines of ~15 characters each than one of concatenation an ternary that takes 70 characters.

### Each tag, each class
Most of the tags have their own class which let's you use them for what they need to be used. As example, you would never set a value attribute to a div. Let's see some examples.
```php
>'
$tag->href('http://google.ca'); //

$tag = new Input; // ''
$tag = new Input('password'); // ''

$tag = new Select([ //
'-1'=>'Refused', // Accepted
'0'=>'Pending', // Accepted
'1'=>'Accepted' // Accepted
]); //
```

### Nesting is fun
You can easily nest tags, let's nest some stuff together

```php
child(
(new Div('second'))->child(
(new Div('third'))->content('Tada, stuff!')
)
);

echo $div->make();

/*
Will result in




Tada, stuff!



*/
```

### Custom tag list

Let's say for example you would use the bootstrap button sometimes but don't want to always rewrite it. You could create a custom tag, like this one
```php
classes([
'btn',
($size != '') ? 'btn-'.$size : '',
'btn-'.$color
]);
}
}

// And you can easily use it
use MyNamespace\BsButton;

(new BsButton)->make() //
(new BsButton('danger')) //

```

## Conclusion

Thank you for using, testing and improving it and feel free to contact me for any question.

Ending joke :
> A QA engineer enter a bar
> He order 1 beer
> He order 0.3 beer
> He order null beer
> He order a lizard
> He order ¡ beer