1. Go to this page and download the library: Download oscarotero/html library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
oscarotero / html example snippets
namespace Html;
//Create a div
$div = div();
//Render the div
echo $div;
//<div></div>
//Create a div with text content
echo div('Hello world');
//<div>Hello world</div>
//Create a div with more text content
echo div('Hello', ' world');
//<div>Hello world</div>
//HTML entities are escaped
echo div('Hello', ' <world>');
//<div>Hello <world></div>
//Use the function raw() to do not escape html entities
echo div('Hello', raw(' <world>'));
//<div>Hello <world></div>
//Create a div with html content
echo div('Hello ', strong('world'));
//<div>Hello <strong>world</strong></div>
//A div with many content
echo div(
h1('Hello world'),
p('This is a paragraph'),
ul(
li('And this is a list item'),
li('Other list item')
)
);
//Add attributes using an array
echo div(['id' => 'foo'], 'Hello world');
//Or add attributes with magic methods
echo div('Hello world')->id('foo');
//Both examples outputs: <div id="foo">Hello world</div>
//Add attributes using an array
echo div(['id' => null], 'Hello world');
//Or add attributes with magic methods
echo div('Hello world')->id(null);
//Both examples outputs: <div>Hello world</div>
//Positive flag
echo div(['hidden' => true]);
//<div hidden></div>
//Negative flag
echo div(['hidden' => false]);
//<div></div>
//A short method to add positive flags:
echo div(['hidden']);
//<div hidden></div>
//Mixing attributes and flags
echo div(['hidden', 'class' => 'foo']);
//<div hidden class="foo"></div>
//Positive flag
echo div()->hidden(true);
//<div hidden></div>
//Negative flag
echo div()->hidden(false);
//<div></div>
//A short method to add positive flags (true is not needed):
echo div()->hidden();
//<div hidden></div>
$article = article(
header(
h1('Hello world')
),
div(['class' => 'content'],
p('This is an article')
)
);
//Export to JSON
$json = json_encode($article);
//Use the function array2Html to recreate the html from json:
$article = array2Html(json_decode($json, true));
//Serialize and unserialize
$ser = serialize($article);
$article = unserialize($ser);