yukanoe/html

PHP 库 - 最小化、简单和可移植的 DOM。

1.0.10 2024-09-28 14:33 UTC

This package is auto-updated.

Last update: 2024-09-28 14:34:14 UTC


README

PHP 库,简单、最小化和可移植的 DOM。

尝试: demo-01.yukanoe.org

1 安装

composer require yukanoe/html

2 目录表

...

3 HTML->PHP 与 TagManager

目录: /examples/project

3.1 目录 & 文件结构

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

3.2 创建构建工具 - 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 创建 HTML 文件

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 创建 PHP 文件

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 运行构建工具 & 内置 Web 服务器

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

3.6 打开

4 标签用法

use \Yukanoe\HTML\Tag;

构造函数

$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 (浏览器)

<!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();

链接方法

addChild() x 3.1415926535897932384626433

$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;
}

DOMDocument

php 8.3 及以下 - 默认字符集 ISO-8859-1

DOMDocument::loadHTML 将将你的字符串视为 ISO-8859-1(HTTP/1.1 默认字符集),除非你明确告知否则。这导致 UTF-8 字符串被错误解释。

必选

php 8.4 - DOM HTML5 解析和序列化

https://wiki.php.net/rfc/domdocument_html5_parser https://wiki.php.net/todo/php84 - 2024年7月16日 特性冻结 如何改进 PHP 8.4 以提供更好的支持解析和处理 HTML5 页面和文件

作者

kirishimayuu (kirishimayuu@yukanoe.org)

许可

Yukanoe\HTML 在 MIT 许可证下发布 - 详细信息请参阅 LICENSE 文件。