HTML 요소를 생성하는 경량 PHP 클래스입니다.
이 패키지에는 php-5.4 이외의 종속성이 없습니다.
Composer는 PHP 패키지에 널리 사용되는 종속성 관리자입니다. 이 Html-Form은 Packagist에서 user-meta/html
로 제공되며 composer require
명령을 실행하여 설치할 수 있습니다. 프로젝트에 Composer를 활성화하려면 프로젝트의 시작하기 문서를 참조하세요.
명령을 사용하여 이 종속성을 추가하려면 프로젝트 디렉터리 내에서 다음을 실행합니다.
composer require user-meta/html
작곡가를 사용하여 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);
이름, ID, 클래스 또는 기타 속성을 할당하려면 두 번째 인수( $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 >
컬렉션은 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 태그에 Html::tag()
사용합니다.
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
값 속성에 적용합니다. esc_url
href
및 src
속성으로 변경합니다.