用于生成 html 元素的轻量级 PHP 类。
该软件包除了 php-5.4 之外没有任何依赖项
Composer 是广泛使用的 PHP 包依赖管理器。此 Html-Form 在 Packagist 上以user-meta/html
提供,并且可以通过运行composer require
命令来安装。要为您的项目启用 Composer,请参阅项目的入门文档。
要使用命令添加此依赖项,请从项目目录中运行以下命令:
composer require user-meta/html
使用 Composer 通过 Html-Form 生成文本输入的快速示例:
<?php
require __DIR__ . ' /vendor/autoload.php ' ;
use UserMeta Html Html ;
echo Html:: text ( ' example ' );
输出:
< input type =" text " value =" example " />
几乎所有类型的 html 元素都可以使用。 (例如:按钮、电子邮件、div、p 等)
echo Html:: button ( ' Submit Me ' );
echo Html:: email ( ' example email ' );
echo Html:: div ( ' example text ' );
echo Html:: p ( ' example text ' );
输出:
< input type =" button " value =" Submit Me " />
< input type =" email " value =" example email " />
< div > example text </ div >
< p > example text </ p >
大多数元素接受两个参数:
$default
:默认值$attributes
:属性数组echo Html::text($default, attributes);
对于像 select、radio 这样的选项元素,它接受第三个参数作为$options
$options
:选项数组。数组可以包含键值对或仅包含值echo Html::select($default, attributes, $options);
要分配 name、id、class 或任何其他属性,请使用第二个参数 ( $attributes
)
具有默认值、名称、id 和类属性的文本字段:
echo Html:: text ( ' Example_Value ' , [ ' name ' => ' Example_Name ' , ' id ' => ' Example_ID ' , ' class ' => ' Example_Class ' ]);
输出:
< input type =" text " name =" Example_Name " value =" Example_Value " id =" Example_ID " class =" Example_Class " />
您还可以将任何属性添加到任何元素中:
echo Html:: text ( ' Example_Value ' , [ ' name ' => ' Example_Name ' , ' data-example ' => ' Example_Data ' ]);
输出:
< input type =" text " name =" Example_Name " value =" Example_Value " data-example =" Example_Data " />
echo Html:: email ( null , [ ' name ' => ' Email ' , ' required ' ]);
echo Html:: email ( null , [ ' name ' => ' Email ' , ' readonly ' ]);
echo Html:: email ( null , [ ' name ' => ' Email ' , ' disabled ' ]);
输出:
< input type =" email " name =" Email " required =" required " />
< input type =" email " name =" Email " readonly =" readonly " />
< input type =" email " name =" Email " disabled =" disabled " />
echo Html:: email ( null , [
' name ' => ' Example_Name ' ,
' label ' => ' Email '
]);
echo Html:: email ( null , [
' name ' => ' Example_Name ' ,
' label ' => [
' Example ' ,
' class ' => ' Class '
]
]);
输出:
< label > Email </ label >
< input type =" email " name =" Example_Name " />
< label class =" Class " > Example </ label >
< input type =" email " name =" Example_Name " />
echo Html:: div ( ' example text ' , [ ' id ' => ' Example_ID ' , ' class ' => ' Example_Class ' ]);
输出:
< div id =" Example_ID " class =" Example_Class " > example text </ div >
echo Html:: label ( ' Some text ' , [ ' id ' => ' ID ' , ' class ' => ' Class ' , ' for ' => ' For ' ]);
输出:
< label id =" ID " class =" Class " for =" For " > Some text </ label >
echo Html:: checkbox ( true , [ ' name ' => ' Name ' ]);
echo Html:: checkbox ( true , [ ' name ' => ' Name ' , ' value ' => ' Value ' ]);
输出:
< input type =" checkbox " name =" Name " value =" 1 " checked =" checked " />
< input type =" checkbox " name =" Name " value =" Value " checked =" checked " />
将第一个参数传递为 false 以表示默认未选中。 echo Html::checkbox(false)
创建具有默认值的复选框列表
echo Html:: checkbox ( ' cat ' , [ ' name ' => ' Name ' , ' id ' => ' ID ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);
echo Html:: checkbox ([ ' cat ' ], [ ' name ' => ' Name ' , ' id ' => ' ID ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);
输出
< label > < input type =" checkbox " value =" dog " name =" Name " id =" ID_1 " /> Dog </ label >
< label > < input type =" checkbox " value =" cat " name =" Name " id =" ID_2 " checked =" checked " /> Cat </ label >
通过 POST 或 GET 方法获取值数组
echo Html:: checkbox ([ ' cat ' ], [ ' name ' => ' Name[] ' , ' id ' => ' ID ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);
输出
< label > < input type =" checkbox " value =" dog " name =" Name[] " id =" ID_1 " /> Dog </ label >
< label > < input type =" checkbox " value =" cat " name =" Name[] " id =" ID_2 " checked =" checked " /> Cat </ label >
echo Html:: select ([ ' cat ' ], [ ' name ' => ' Name ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);
echo Html:: select ([ ' cat ' ], [ ' name ' => ' Name ' ], [ ' dog ' , ' cat ' ]);
输出
< select name =" Name " >
< option value =" dog " > Dog </ option >
< option value =" cat " selected =" selected " > Cat </ option >
</ select >
< select name =" Name " >
< option value =" dog " > dog </ option >
< option value =" cat " selected =" selected " > cat </ option >
</ select >
echo Html:: radio ([ ' cat ' ], [ ' name ' => ' Name ' , ' id ' => ' ID ' ], [ ' dog ' , ' cat ' ]);
输出
< label > < input type =" radio " value =" dog " name =" Name " id =" ID_1 " /> dog </ label >
< label > < input type =" radio " value =" cat " name =" Name " id =" ID_2 " checked =" checked " /> cat </ label >
多个元素可以组合在一起作为集合
$ div = new Html ( ' div ' );
$ div -> p ( ' Hello World ' );
$ div -> text ( ' example ' );
$ div -> add ( ' Some plain text ' );
echo $ div -> render ();
输出:
< div >
< p > Hello World </ p >
< input type =" text " value =" example " />
Some plain text
</ div >
Collection 使用Html
构造函数并接受两个参数。
$type
(可选):标签名称。 (例如表单、div)$attributes
(可选):属性数组使用集合生成表单:
$ form = new Html ( ' form ' , [ ' method ' => ' POST ' ]);
$ form -> div ( ' Enter your email and password for login ' );
$ form -> email ( '' , [ ' name ' => ' email ' , ' label ' => ' Email ' ]);
$ form -> password ( '' , [ ' name ' => ' password ' , ' label ' => ' Password ' ]);
$ form -> submit ( ' login ' );
echo $ form -> render ();
输出:
< form method =" POST " >
< div > Enter your email and password for login </ div >
< label > Email </ label >
< input type =" email " name =" email " />
< label > Password </ label >
< input type =" password " name =" password " />
< input type =" submit " value =" login " />
</ form >
使用嵌套集合生成 html 模板:
$ html = new Html ( ' html ' );
$ head = $ html -> import ( ' head ' );
$ head -> title ( ' Example Title ' );
$ body = $ html -> import ( ' body ' );
$ body -> p ( ' Hello World ' );
echo $ html -> render ();
< html >
< head >
< title > Example Title </ title >
</ head >
< body >
< p > Hello World </ p >
</ body >
</ html >
$ book = new Html ( ' book ' );
$ book -> title ( ' The Da Vinci Code ' );
$ author = $ book -> import ( ' author ' );
$ author -> name ( ' Dan Brown ' );
$ author -> nationality ( ' American ' );
echo $ book -> render ();
输出
< book >
< title >The Da Vinci Code</ title >
< author >
< name >Dan Brown</ name >
< nationality >American</ nationality >
</ author >
</ book >
可以通过调用其名称来创建任何 html 元素。
echo Html:: email ( ' [email protected] ' );
echo Html:: h1 ( ' Example Heading ' );
在底层,我们使用Html::input()
作为输入元素, Html::tag()
作为 html 标签
使用input
法创建电子邮件输入:
echo Html:: input ( ' email ' , ' [email protected] ' );
使用tag
方法创建h1:
echo Html:: tag ( ' h1 ' , ' Example Heading ' );
echo Html:: email ( '' , [ ' _before ' => ' Before ' , ' _after ' => ' After ' ]);
输出
Before < input type =" email " /> After
echo Html:: email ( '' , [ ' _enclose ' => ' div ' ]);
echo Html:: email ( '' , [ ' _enclose ' => [ ' div ' , ' class ' => ' Class ' ]]);
输出
< div >
< input type =" email " />
</ div >
< div class =" Class " >
< input type =" email " />
</ div >
// Same value and label
echo Html:: select ( null , [], [ ' audi ' , ' bmw ' ]);
// Different value and label
echo Html:: select ( null , [], [ ' audi ' => ' Audi ' , ' bmw ' => ' BMW ' ]);
// Option with extra attributes
echo Html:: select ( null , [], [ ' ferrari ' => [ ' Ferrari ' , ' data-origin ' => ' Italy ' ]]);
echo Html:: select ( null , [], [[ ' value ' => ' ferrari ' , ' label ' => ' Ferrari ' , ' data-origin ' => ' Italy ' ]]);
输出
< select > < option value =" audi " > audi </ option > < option value =" bmw " > bmw </ option > </ select >
< select > < option value =" audi " > Audi </ option > < option value =" bmw " > BMW </ option > </ select >
< select > < option value =" ferrari " data-origin =" Italy " > Ferrari </ option > </ select >
< select > < option value =" ferrari " data-origin =" Italy " > Ferrari </ option > </ select >
将多种方式与一个选项数组混合
echo Html:: select ( null , [], [
' audi ' ,
' bmw ' => ' BMW ' ,
' honda ' => [
' Honda ' ,
' data-origin ' => ' Japan '
],
[
' value ' => ' ferrari ' ,
' label ' => ' Ferrari ' ,
' data-origin ' => ' Italy '
]
]);
输出
< select >
< option value =" audi " > audi </ option >
< option value =" bmw " > BMW </ option >
< option value =" honda " data-origin =" Japan " > Honda </ option >
< option value =" ferrari " data-origin =" Italy " > Ferrari </ option >
</ select >
使用数值
echo Html:: select ( null , [], [ 2 => ' Two ' , 4 => ' Four ' ]);
输出
< select >
< option value =" 2 " > Two </ option >
< option value =" 4 " > Four </ option >
</ select >
转义意味着删除不需要的数据,例如格式错误的 HTML 或脚本标签。
该库将esc_attr
应用于 value 属性。 esc_url
到href
和src
属性。