joaorobertopb/htmlable

Simple PHP library for abstraction and rendering of html elements.

1.1.0 2018-05-31 19:25 UTC

This package is auto-updated.

Last update: 2024-10-29 05:10:36 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version Code Coverage Quality Score Software License

Simple PHP library for abstraction and rendering of html elements.

Install

Via Composer

$ composer require joaorobertopb/htmlable

Usage

$div = new JoaoRobertoPB\Htmlable\Tag('div');
$div->add('Hello, Htmlable!');
$div->render();
  • The html code will be printed by the methodrender().
  • To return the html code use the toHtml() method.

Examples

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>

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Credits

This package is inspired by this great book by @pablodalloglio. Here is a package I used as reference.

License

The MIT License (MIT). Please see License File for more information.