A PHP library , Simple, minimal and portable DOM.
Try: demo-01.yukanoe.org
composer require yukanoe/html
- Overview
- Quick setup guide
- Tag Basic Usage
- Test/Demo Directory
- Examples Directory
- Class Tag
- Class TagManager
- Private HTML Attributes
...
Directory: /examples/project
project/
│ build.php
│ index.php
│
└───template-html/
│ │ index.html
│ │ ...
│
└───template-php/
build.php
require __DIR__.'/vendor/autoload.php';
use \Yukanoe\HTML\TagManager;
$inputDir = './template-html';
$outputDir = './template-php';
$tagManager = new TagManager;
$tagManager->autoBuild($inputDir, $outputDir);
project/html/index.html
<html>
<head>
<title data-yukanoe-id="title">HomePage</title>
</head>
<body>
<div data-yukanoe-id="text">Say a-ny--thin--g--.</div>
</body>
</html>
project/index.php
<?php
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/template-php/index.php';
$username = $_GET['username'] ?? '';
if($username) {
$avn['title']->text = "Hi, {$username}";
$avn['text']->text = "{$username}: Say anything.";
}
cd project/
composer require yukanoe/html
php build.php
php -S localhost:8080 index.php
use \Yukanoe\HTML\Tag;
$myTag = new Tag(String $name, Array $attribute, String $text);
<?php
require __DIR__.'/vendor/autoload.php';
use \Yukanoe\HTML\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]);
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div class="ruby">
<h1>Hello World!</h1>
<p>This is a paragraph.</p>
</div>
</body>
</html>
$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();
$html->addChild($head);
$html->addChild([$head, $body]);
$html->addChild($head, $body);
/examples/example-00.php
$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']} "));
}
/examples/example-00.php
$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);
}
/examples/example-00.php
$user ??= '';
if($user == 'admin'){
$center->attribute['class'] .= ' ruby';
$center->text = $user;
}
kirishimayuu (kirishimayuu@yukanoe.org)
Yukanoe\HTML is licensed under the MIT License - see the LICENSE file for details.