PHP code example of yukanoe / html

1. Go to this page and download the library: Download yukanoe/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/ */

    

yukanoe / html example snippets


use Yukanoe\HTML\Tag;

$myTag = new Tag(string $name='', array $attribute=[],  string $text='');


# index.php
L\Tag;

$html  = new Tag('html', [], '');
$head  = new Tag('head', [], '');
$title = new Tag('title', [], 'Page Title');
$body  = new Tag('body', [], '');
$div   = new Tag('div', ['class'=>'ruby'], '');
$h1    = new Tag('h1', [], 'Hello World!');
$p     = new Tag('p', [], 'This is a paragraph.');
$html->addChild([$head, $body]);
$head->addChild($title);
$body->addChild($div);
$div->addChild([$h1, $p]);

$myTag->name = "div"
// class attribute
$myTag->attribute['class'] = 'card';
// all attribute
$myTag->attribute = ['class'=>'card'];
// text
$myTag->text = 'string 123456789';

$myTag->setName('div');
$myTag->setAttribute(['class'=>'card']);
$myTag->setAttribute('class', 'card');
$myTag->setText('string 123456789');

$myTag->hide();
$myTag->show();

$myTag->restrict(string $scope='restricted');
$myTag->restrict(string $scope='none');

# optional 1: recommended
$html->addChild([$head, $body]);

# optional 2
$html->addChild($head, $body);

# optional 3
$html->addChild($head)->addChild($body);


$messages = [
    ["name" => "admin", "text" => "bar"],
    ["name" => "user1", "text" => "foo"],
    ["name" => "admin", "text" => "barbarbar"],
    ["name" => "user1", "text" => "foofoofoo"]
];
$body->addChild($center = new Tag('div'));
foreach ($messages as $msg) {
  $center->addChild(new Tag('p', [], "{$msg['name']}: {$msg['text']} "));
}

$messages = [
    ["name" => "admin", "text" => "bar"],
    ["name" => "user1", "text" => "foo"],
    ["name" => "admin", "text" => "barbarbar"],
    ["name" => "user1", "text" => "foofoofoo"]
];
$body->addChild($center = new Tag('div'));
$msgDiv  = new Tag('div',  ['class'=>'message'], '');
$msgDiv->addChild([
    new Tag('span', ['style'=>' font-weight: bold; '], ''),
    new Tag('span', [], '')
]);
foreach ( $messages as $msg) {
    $newDivMsg = clone $msgDiv;
    $newDivMsg->child[0]->text = $msg['name'];
    $newDivMsg->child[1]->text = $msg['text'];
    $center->addChild($newDivMsg);
}

$user ??= '';
if($user == 'admin'){
  $center->attribute['class'] .= ' ruby';
  $center->text = $user;
}