Skip to content
/ html Public

A PHP library , Simple, minimal and portable DOM.

License

Notifications You must be signed in to change notification settings

yukanoe/html

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yukanoe\HTML PHP library!

A PHP library , Simple, minimal and portable DOM.

Try: demo-01.yukanoe.org

1 Installation

composer require yukanoe/html

2 Table of Contents

...

3 HTML->PHP with TagManager

Directory: /examples/project

3.1 Directory & File Structure

project/
│   build.php    
│   index.php      
│
└───template-html/
│   │   index.html
│   │   ...
│   
└───template-php/

3.2 Create build tool - build.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); 

3.3 Create html files

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>

3.4 Create php files

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.";
}

3.5 Run build tool & Built-in Web server

cd project/
composer require yukanoe/html
php build.php
php -S localhost:8080 index.php

3.6 Open

4 Tag Usage

Class

use \Yukanoe\HTML\Tag;

Constructor

$myTag = new Tag(String $name, Array $attribute,  String $text);

INPUT (index.php)

<?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]);

OUTPUT (browser)

<!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>

Basic Usage

set attribute

public attributes

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

public methods

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

hide()/show()

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

Link method

addChild() x 3.1415926535897932384626433

$html->addChild($head);
$html->addChild([$head, $body]);
$html->addChild($head, $body);

Loops

/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']} "));
}

Clone ( DEFAULT: deep clone )

/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);
}

Conditional Statements

/examples/example-00.php

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

Author

kirishimayuu (kirishimayuu@yukanoe.org)

License

Yukanoe\HTML is licensed under the MIT License - see the LICENSE file for details.