PHP code example of oscarotero / html

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 &lt;world&gt;</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>

//Using an array
echo div(['class' => ['foo', 'bar']]);

//Using the magic method:
echo div()->class(['foo', 'bar']);

//Both examples return: <div class="foo bar"></div>

//Using an array
echo div([
    'class' => [
        'foo',
        'bar',
        'theme-blue' => true,
        'error' => !empty($error)
    ]
]);

//Using the magic method:
echo div()->class([
    'foo',
    'bar',
    'theme-blue' => true,
    'error' => !empty($error)
]);

//Both examples output: <div class="foo bar theme-blue"></div>

//Using an array
echo div([
    'data-enabled' => true,
    'data-other' => false,
    'data-omitted' => null, //Null values are ommitted
    'data-options' => ['foo', 'bar']
]);

//Using the special method `data()`
echo div()
    ->data('enabled', true),
    ->data('other', false),
    ->data('omitted', null),
    ->data('options', ['foo', 'bar']);

//Both examples output: <div data-enabled="true" data-other="false" data-options="[&quot;foo&quot;,&quot;bar&quot;]"></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);

$div = div('First child', strong('Second child'), 'Third child');

echo $div[2]; //Third child

$div = div('First child', strong('Second child'), 'Third child');

foreach ($div as $child) {
    echo $child;
}

$div = div('First child', strong('Second child'), 'Third child');

echo count($div); //3

$div = div('First child', null, 'Third child');

echo count($div); //2