Simple PHP library for abstraction and rendering of html elements.
Via Composer
$ composer require joaorobertopb/htmlable
$div = new JoaoRobertoPB\Htmlable\Tag('div');
$div->add('Hello, Htmlable!');
$div->render();
- The html code will be printed by the method
render()
. - To return the html code use the
toHtml()
method.
An empty tag:
$div = new Tag('div'); // Or $div = Tag::make('div');
$div->render();
<div></div>
A plain tag with text contents:
$h1 = new Tag('h1');
$h1->add("Header");
$h1->render();
<h1>Header</h1>
A tag with multiple attributes:
$a = new Tag('a');
$a->id = "example";
$a->href = "#";
$a->add("Link");
$a->render();
//Or attributes via construct method
$a = new Tag('a',['id'=>"example", 'href'=>"#"]);
$a->add("Link");
$a->render();
<a id="example" href="#">Link</a>
A more complex structure:
$div = new Tag('div',['class'=>"container"]);
$divRow = new Tag('div',['class'=>"row"]);
$divCol = new Tag('div',['class'=>"col-md-6"]);
$divCol->add("Hello!");
$divRow->add($divCol);
$div->add($divRow);
$div->render();
<div class="container">
<div class="row">
<div class="col-md-6">
Hello!
</div>
</div>
</div>
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING for details.
This package is inspired by this great book by @pablodalloglio. Here is a package I used as reference.
The MIT License (MIT). Please see License File for more information.