dev-master
9999999-devHandle HTML elements as an object with optional nodes
GPL-3.0
The Requires
- php >=5.2.0
by Dian Derksen
Handle HTML elements as an object with optional nodes
Handle HTML elements as an object with optional nodes, (*1)
You can use SimpleHtmlElement by putting the following line into your composer.json, (*2)
{ "require": { "derxen/simple-html-element": "~1.0" } }
And make sure you load the class within you PHP code or use the Composer Autoloader, (*3)
SimpleHtmlElement has been made to make your work easier. Here is an example:, (*4)
Create html root., (*5)
$html = new derxen\SimpleHtmlElement();
We define some table headers., (*6)
$headers = ['publish date', 'title', 'author'];
We fetch some table data., (*7)
$data = []; $data[] = ['05-03-2016', 'A great book', 'Sam Wilson']; $data[] = ['08-04-2016', 'Another great book', 'Jane Fisscher']; $data[] = ['11-01-2016', 'The greatest book', 'Dan Morris'];
Let us create a new table.
If you give an array as property, SimpleHtmlElement will handle the property as attributes
, (*8)
$table = $html->table(['method' => 'post']);
Now we add the table headers., (*9)
foreach($headers as $th) { $table->th($th); }
Now we add the table rows with content.
If you give a string as property, SimpleHtmlElement will handle the property as content
, (*10)
foreach($data as $cols) { $tr = $table->tr(); foreach($cols as $td) { $tr->td($td); } }
We could set some extra attributes to the table., (*11)
$table->setAttribute(['action' => '/handleform']);
Our table is ready. Now we render the table, (*12)
echo $table->write();
Or We can just do:, (*13)
echo $table;
Handle HTML elements as an object with optional nodes
GPL-3.0