lib16/html

lib16 HTML Builder 是一个用于创建 HTML5 文档的 PHP 8 库。

dev-main 2021-01-01 17:58 UTC

This package is auto-updated.

Last update: 2024-08-29 05:34:41 UTC


README

一个用 PHP 8 编写的创建 HTML5 的库。

Build Status Coverage

使用 Composer 安装

此包可在 packagist 上找到,因此您可以使用 Composer 来安装它。在您的 shell 中运行以下命令

composer require lib16/html dev-main

基本用法

使用此库构建您的表格

<?php
require_once 'vendor/autoload.php';

use Lib16\HTML\Table;

$data = [
    ['model' => 'Panther', 'city' => 'Berlin', 'quantity/store' => 20],
    ['model' => 'Panther', 'city' => 'Berlin', 'quantity/store' => 12],
    ['model' => 'Panther', 'city' => 'Cologne', 'quantity/store' => 12],
    ['model' => 'Lion', 'city' => 'Cologne', 'quantity/store' => 12],
    ['model' => 'Lion', 'city' => 'Hamburg', 'quantity/store' => 15],
    ['model' => 'Lion', 'city' => 'Hamburg', 'quantity/store' => 15],
    ['model' => 'Lion', 'city' => 'Munich', 'quantity/store' => 15],
];
$table = Table::create('Availability')->setClass('t1');
$table->thead()->tr()->thn('model', 'city', 'quantity/store');
$table->bodies($data);
print $table;

// change order
$keys = ['city', 'model', 'quantity/store'];
$table = Table::create('Availability')->setClass('t1');
$table->thead()->tr()->headerCells($keys);
$table->bodies($data, ...$keys);
print $table;

// row by row
$table = Table::create('Availability')->setClass('t2');
$table->thead()->tr()->thn('model', 'city', 'quantity/store');
foreach ($data as $row) {
    $table->tr()->dataCells($row);
}
print $table;

?>
<style>
body, * {
    font-family: source serif pro, serif;
}
caption, th {
    font-family: source sans pro, sans-serif;
}
caption {
    margin: 0.5em;
}
td, th {
    text-align: left;
    vertical-align: text-top;
    padding: 0.5em;
    border: 1px solid rgb(0, 51, 102);
}
td:last-child {
    text-align: right;
}
th {
    color: white;
    background-color: rgba(0, 51, 102, 1);
    font-weight: normal;
}
.t1 tbody,
.t2 tr {
    background-color: rgba(51, 102, 0, 0.2);
}
.t1 tbody:nth-child(2n),
.t2 tr:nth-child(2n) {
    background-color: rgba(52, 102, 0, 0.3);
}
table {
    border-collapse: collapse;
    margin: 2em;
    float: left;
}
</style>