Classes PHP leves para gerar elementos HTML.
Este pacote não possui nenhuma dependência além do php-5.4
Composer é um gerenciador de dependências amplamente utilizado para pacotes PHP. Este formulário HTML está disponível no Packagist como user-meta/html e pode ser instalado executando o comando composer require . Para habilitar o Composer para seu projeto, consulte a documentação de primeiros passos do projeto.
Para adicionar essa dependência usando o comando, execute o seguinte no diretório do projeto:
composer require user-meta/html
Um exemplo rápido de geração de entrada de texto com HTML-Form usando o compositor:
<?php
require __DIR__ . ' /vendor/autoload.php ' ;
use UserMeta Html Html ;
echo Html:: text ( ' example ' );Saída:
< input type =" text " value =" example " />Quase todos os tipos de elementos HTML podem ser usados. (ex.: botão, email, div, p etc)
echo Html:: button ( ' Submit Me ' );
echo Html:: email ( ' example email ' );
echo Html:: div ( ' example text ' );
echo Html:: p ( ' example text ' );Saída:
< input type =" button " value =" Submit Me " />
< input type =" email " value =" example email " />
< div > example text </ div >
< p > example text </ p > A maior parte do elemento aceita dois argumentos:
$default : valor padrão$attributes : array de atributosecho Html::text($default, attributes); Para elementos de opções como select, radio, aceita o terceiro argumento como $options
$options : Conjunto de opções. A matriz pode conter pares de valores-chave ou apenas valoresecho Html::select($default, attributes, $options); Para atribuir nome, id, classe ou qualquer outro atributo, use segundos argumentos ( $attributes )
Um campo de texto com atributos padrão de valor, nome, id e classe:
echo Html:: text ( ' Example_Value ' , [ ' name ' => ' Example_Name ' , ' id ' => ' Example_ID ' , ' class ' => ' Example_Class ' ]);Saída:
< input type =" text " name =" Example_Name " value =" Example_Value " id =" Example_ID " class =" Example_Class " /> Você também pode adicionar quaisquer atributos em qualquer elemento:
echo Html:: text ( ' Example_Value ' , [ ' name ' => ' Example_Name ' , ' data-example ' => ' Example_Data ' ]);Saída:
< 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 ' ]);Saída:
< 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 '
]
]);Saída:
< 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 ' ]);Saída:
< div id =" Example_ID " class =" Example_Class " > example text </ div > echo Html:: label ( ' Some text ' , [ ' id ' => ' ID ' , ' class ' => ' Class ' , ' for ' => ' For ' ]);Saída:
< label id =" ID " class =" Class " for =" For " > Some text </ label > echo Html:: checkbox ( true , [ ' name ' => ' Name ' ]);
echo Html:: checkbox ( true , [ ' name ' => ' Name ' , ' value ' => ' Value ' ]);Saída:
< input type =" checkbox " name =" Name " value =" 1 " checked =" checked " />
< input type =" checkbox " name =" Name " value =" Value " checked =" checked " /> Passe o primeiro argumento como falso para o padrão desmarcado. echo Html::checkbox(false)
Crie uma lista de caixas de seleção com valores padrão
echo Html:: checkbox ( ' cat ' , [ ' name ' => ' Name ' , ' id ' => ' ID ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);
echo Html:: checkbox ([ ' cat ' ], [ ' name ' => ' Name ' , ' id ' => ' ID ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);Saída
< 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 >Para obter uma matriz de valores pelo método POST ou GET
echo Html:: checkbox ([ ' cat ' ], [ ' name ' => ' Name[] ' , ' id ' => ' ID ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);Saída
< 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 ' ]);Saída
< 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 ' ]);Saída
< 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 >Vários elementos podem ser agrupados como coleção
$ div = new Html ( ' div ' );
$ div -> p ( ' Hello World ' );
$ div -> text ( ' example ' );
$ div -> add ( ' Some plain text ' );
echo $ div -> render ();Saída:
< div >
< p > Hello World </ p >
< input type =" text " value =" example " />
Some plain text
</ div > A coleção usa o construtor Html e aceita dois parâmetros.
$type (opcional): nome da tag. (por exemplo, formulário, div)$attributes (opcional): array de atributos Gerando um formulário usando coleções:
$ 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 ();Saída:
< 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 > Gerando modelo HTML usando coleções aninhadas:
$ 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 ();Saída
< book >
< title >The Da Vinci Code</ title >
< author >
< name >Dan Brown</ name >
< nationality >American</ nationality >
</ author >
</ book >É possível criar qualquer elemento html chamando seu nome.
echo Html:: email ( ' [email protected] ' );
echo Html:: h1 ( ' Example Heading ' ); Nos bastidores, usamos Html::input() para elemento de entrada e Html::tag() para tag html
Crie uma entrada de e-mail usando o método input :
echo Html:: input ( ' email ' , ' [email protected] ' ); Crie h1 usando o método tag :
echo Html:: tag ( ' h1 ' , ' Example Heading ' ); echo Html:: email ( '' , [ ' _before ' => ' Before ' , ' _after ' => ' After ' ]);Saída
Before < input type =" email " /> After echo Html:: email ( '' , [ ' _enclose ' => ' div ' ]);
echo Html:: email ( '' , [ ' _enclose ' => [ ' div ' , ' class ' => ' Class ' ]]);Saída
< 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 ' ]]);Saída
< 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 >Misturando várias maneiras com um array de opções
echo Html:: select ( null , [], [
' audi ' ,
' bmw ' => ' BMW ' ,
' honda ' => [
' Honda ' ,
' data-origin ' => ' Japan '
],
[
' value ' => ' ferrari ' ,
' label ' => ' Ferrari ' ,
' data-origin ' => ' Italy '
]
]);Saída
< 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 >Usando valor numérico
echo Html:: select ( null , [], [ 2 => ' Two ' , 4 => ' Four ' ]);Saída
< select >
< option value =" 2 " > Two </ option >
< option value =" 4 " > Four </ option >
</ select > Escapar significa remover dados indesejados, como HTML malformado ou tags de script.
A biblioteca aplica esc_attr ao atributo de valor. esc_url para atributos href e src .