Clases PHP ligeras para generar elementos html.
Este paquete no tiene ninguna dependencia más que php-5.4
Composer es un administrador de dependencias ampliamente utilizado para paquetes PHP. Este formulario HTML está disponible en Packagist como user-meta/html
y se puede instalar ejecutando el comando composer require
. Para habilitar Composer para su proyecto, consulte la documentación de introducción del proyecto.
Para agregar esta dependencia usando el comando, ejecute lo siguiente desde el directorio de su proyecto:
composer require user-meta/html
Un ejemplo rápido de cómo generar entrada de texto con Html-Form usando Composer:
<?php
require __DIR__ . ' /vendor/autoload.php ' ;
use UserMeta Html Html ;
echo Html:: text ( ' example ' );
Producción:
< input type =" text " value =" example " />
Se pueden utilizar casi todos los tipos de elementos html. (por ejemplo: botón, correo electrónico, div, p, etc.)
echo Html:: button ( ' Submit Me ' );
echo Html:: email ( ' example email ' );
echo Html:: div ( ' example text ' );
echo Html:: p ( ' example text ' );
Producción:
< input type =" button " value =" Submit Me " />
< input type =" email " value =" example email " />
< div > example text </ div >
< p > example text </ p >
La mayor parte del elemento acepta dos argumentos:
$default
: valor predeterminado$attributes
: Matriz de atributosecho Html::text($default, attributes);
Para elementos de opciones como select, radio, acepta el tercer argumento como $options
$options
: conjunto de opciones. La matriz puede contener pares clave-valor o solo valores.echo Html::select($default, attributes, $options);
Para asignar nombre, identificación, clase o cualquier otro atributo, use segundos argumentos ( $attributes
)
Un campo de texto con atributos de valor, nombre, identificación y clase predeterminados:
echo Html:: text ( ' Example_Value ' , [ ' name ' => ' Example_Name ' , ' id ' => ' Example_ID ' , ' class ' => ' Example_Class ' ]);
Producción:
< input type =" text " name =" Example_Name " value =" Example_Value " id =" Example_ID " class =" Example_Class " />
También puedes agregar cualquier atributo a cualquier elemento:
echo Html:: text ( ' Example_Value ' , [ ' name ' => ' Example_Name ' , ' data-example ' => ' Example_Data ' ]);
Producción:
< 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 ' ]);
Producción:
< 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 '
]
]);
Producción:
< 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 ' ]);
Producción:
< div id =" Example_ID " class =" Example_Class " > example text </ div >
echo Html:: label ( ' Some text ' , [ ' id ' => ' ID ' , ' class ' => ' Class ' , ' for ' => ' For ' ]);
Producción:
< label id =" ID " class =" Class " for =" For " > Some text </ label >
echo Html:: checkbox ( true , [ ' name ' => ' Name ' ]);
echo Html:: checkbox ( true , [ ' name ' => ' Name ' , ' value ' => ' Value ' ]);
Producción:
< input type =" checkbox " name =" Name " value =" 1 " checked =" checked " />
< input type =" checkbox " name =" Name " value =" Value " checked =" checked " />
Pase el primer argumento como falso para que el valor predeterminado no esté marcado. echo Html::checkbox(false)
Crear una lista de casillas de verificación con valores predeterminados
echo Html:: checkbox ( ' cat ' , [ ' name ' => ' Name ' , ' id ' => ' ID ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);
echo Html:: checkbox ([ ' cat ' ], [ ' name ' => ' Name ' , ' id ' => ' ID ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);
Producción
< 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 obtener una matriz de valores mediante el método POST o GET
echo Html:: checkbox ([ ' cat ' ], [ ' name ' => ' Name[] ' , ' id ' => ' ID ' ], [ ' dog ' => ' Dog ' , ' cat ' => ' Cat ' ]);
Producción
< 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 ' ]);
Producción
< 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 ' ]);
Producción
< 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 >
Se pueden agrupar varios elementos como colección.
$ div = new Html ( ' div ' );
$ div -> p ( ' Hello World ' );
$ div -> text ( ' example ' );
$ div -> add ( ' Some plain text ' );
echo $ div -> render ();
Producción:
< div >
< p > Hello World </ p >
< input type =" text " value =" example " />
Some plain text
</ div >
La colección utiliza el constructor Html
y acepta dos parámetros.
$type
(opcional): nombre de la etiqueta. (por ejemplo, formulario, div)$attributes
(opcional): conjunto de atributos Generando un formulario usando colecciones:
$ 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 ();
Producción:
< 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 >
Generando plantilla html usando colecciones anidadas:
$ 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 ();
Producción
< book >
< title >The Da Vinci Code</ title >
< author >
< name >Dan Brown</ name >
< nationality >American</ nationality >
</ author >
</ book >
Es posible crear cualquier elemento html llamando su nombre.
echo Html:: email ( ' [email protected] ' );
echo Html:: h1 ( ' Example Heading ' );
Debajo del capó, usamos Html::input()
para el elemento de entrada y Html::tag()
para la etiqueta html.
Cree una entrada de correo electrónico utilizando el método input
:
echo Html:: input ( ' email ' , ' [email protected] ' );
Cree h1 usando el método tag
:
echo Html:: tag ( ' h1 ' , ' Example Heading ' );
echo Html:: email ( '' , [ ' _before ' => ' Before ' , ' _after ' => ' After ' ]);
Producción
Before < input type =" email " /> After
echo Html:: email ( '' , [ ' _enclose ' => ' div ' ]);
echo Html:: email ( '' , [ ' _enclose ' => [ ' div ' , ' class ' => ' Class ' ]]);
Producción
< 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 ' ]]);
Producción
< 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 >
Mezclar de varias maneras con una matriz de opciones
echo Html:: select ( null , [], [
' audi ' ,
' bmw ' => ' BMW ' ,
' honda ' => [
' Honda ' ,
' data-origin ' => ' Japan '
],
[
' value ' => ' ferrari ' ,
' label ' => ' Ferrari ' ,
' data-origin ' => ' Italy '
]
]);
Producción
< 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 ' ]);
Producción
< select >
< option value =" 2 " > Two </ option >
< option value =" 4 " > Four </ option >
</ select >
Escapar significa eliminar datos no deseados, como HTML con formato incorrecto o etiquetas de script.
La biblioteca aplica esc_attr
al atributo de valor. esc_url
a los atributos href
y src
.