tyokinuhata/html-builder

一个可以在PHP中编写HTML的库

v1.3.2 2018-06-19 06:09 UTC

This package is not auto-updated.

Last update: 2024-10-02 20:24:02 UTC


README

PHP中构建HTML的库

使用此库的情况

安装

$ composer require tyokinuhata/html-builder

示例代码

<?php

require_once 'vendor/autoload.php';

$hb = new HtmlBuilder\HtmlBuilder();

$hb
->html('lang', 'ja')
    ->head()
        ->meta('charset', 'utf-8')
        ->title()
            ->text('タイトル')->upstream()
        ->style([
            '#title' => [
                'color' => 'white',
                'background' => 'red'
            ],
            '#content' => [
                'color' => 'green',
                'background' => 'blue'
            ]
        ])->upstream()
    ->body()
        ->h1('id', 'title')
            ->text('見出し')->upstream()
        ->p('id', 'content')
            ->text('本文')->upstream()
        ->input('type', 'text', '@autofocus')
        ->button('type', 'submit')
            ->text('送信')->append();

基本来说,元素是以 ->元素名() 的形式来描述的。
对于需要属性和值的元素,可以按照 ->元素名('属性', '值') 的方式来描述。
例外情况下,对于不需要值的属性,可以按照 ->元素名('@属性') 的方式来描述。

对于纯文本描述,可以按照 ->text('文本') 的方式,传递想要绘制的文本作为参数。

CSS的描述可以是,如 ->div('style', '属性:值;')->link('rel', '.css文件路径'),也可以是 ->style(['选择器' => ['属性' => '值']) 的形式。

->upstream() 是用于跳出当前层级的。
要跳出多个层级,可以按照 ->upstream(2) 的方式,指定要跳出的层级数作为参数。

->build('路径') 是一个用于构建的方法。
该方法会根据指定的路径,基于PHP文件生成HTML文件。

->append() 是一个输出方法。

启动内置服务器的步骤

$ php -S localhost:8000 index.php

可用的元素列表

  • html
  • head
  • title
  • link
  • meta
  • style
  • script
  • body
  • section
  • nav
  • article
  • aside
  • h1 ~ h6
  • header
  • footer
  • p
  • hr
  • pre
  • ol
  • ul
  • li
  • dl
  • dt
  • dd
  • div
  • main
  • a
  • strong
  • sub
  • sup
  • i
  • b
  • span
  • br
  • wbr
  • img
  • iframe
  • table
  • caption
  • col
  • tbody
  • thead
  • tfoot
  • tr
  • td
  • th
  • form
  • label
  • input
  • button
  • select
  • option
  • textarea

开发此库的情况

克隆

$ git clone https://github.com/tyokinuhata/html-builder.git

设置

$ make setup

启动内置服务器的步骤

$ make up

各个文件的说明

链接

GitHub
Packagist