HelPHP

Html
in package

Tags
class

HTML

it's the base class for all HTML tags and elements. It allows to create HTML tags and elements, to add attributes, children,etc. it's main purpose is to generate HTML code in a structured way, and manipulate it easily as an object, and permit to avoid direct html writing inside PHP for a better reading of the code, but also to push all the display at the end of the data processing.

It can be used to create complex HTML structures, such as forms, tables, lists, etc. And it's the fundation of the H class that is creating more complex html object.

all constants are there to bridge to html tags, if html evolve or if you want to connect to another lib display

this class use magic methods to help manipulate html object.

see
__get()
see
__set()
see
__unset()

Table of Contents

Constants

_CHECKMARK_BOLD  = '✔'
_CHECKMARK_NORMAL  = '✓'
_DOWN_ARROW  = '↓'
_DOWN_DASHED_ARROW  = '⇣'
_LEFT_ARROW  = '←'
_LEFT_DASHED_ARROW  = '⇠'
_RIGHT_ARROW  = '→'
_RIGHT_ARROW_HEAD  = '➤'
_RIGHT_DASHED_ARROW  = '⇢'
_STAR_EMPTY  = '☆'
_STAR_FULL  = '★'
_UP_ARROW  = '↑'
_UP_DASHED_ARROW  = '⇡'
_X_BOLD  = '✖'
_X_NORMAL  = '✕'
A  = 'a'
AUDIO  = 'audio'
BODY  = 'body'
BR  = 'br'
BUTTON  = 'button'
CANVAS  = 'canvas'
DATALIST  = 'datalist'
DETAILS  = 'details'
DIV  = 'div'
FIELDSET  = 'fieldset'
FORM  = 'form'
HEAD  = 'head'
HR  = 'hr'
HTML  = 'html'
IMAGE  = 'IMAGE'
IMG  = 'img'
INPUT  = 'input'
LABEL  = 'label'
LEGEND  = 'legend'
LI  = 'li'
LINK  = 'link'
META  = 'meta'
OPTGROUP  = 'optgroup'
OPTION  = 'option'
PARAM  = 'param'
QUICK_EDIT_DATA  = 'qe_d'
QUICK_EDIT_FIELD  = 'qe_f'
QUICK_EDIT_ID  = 'qe_i'
QUICK_EDIT_TYPE  = 'qe_t'
QUICK_EDIT_TYPE_INDEX  = 'qe_ti'
SCRIPT  = 'script'
SELECT  = 'select'
simple_tags  = [\helPHP\libs\Html::IMG, \helPHP\libs\Html::IMAGE, \helPHP\libs\Html::BR, \helPHP\libs\Html::HR, \helPHP\libs\Html::INPUT, \helPHP\libs\Html::LINK, \helPHP\libs\Html::META, \helPHP\libs\Html::PARAM, \helPHP\libs\Html::USE]
SOURCE  = 'source'
SPAN  = 'span'
STYLE  = 'style'
SUMMARY  = 'summary'
SVG  = 'svg'
TABLE  = 'table'
TBODY  = 'tbody'
TD  = 'td'
TEXTAREA  = 'textarea'
TFOOT  = 'tfoot'
TH  = 'th'
THEAD  = 'thead'
TITLE  = 'title'
TR  = 'tr'
UL  = 'ul'
USE  = 'use'
VIDEO  = 'video'

Properties

$children  : mixed
$current_value  : mixed
$current_value_label  : mixed
$debug  : mixed
$extra  : mixed
$items_after  : mixed
$label  : mixed
$label_for  : mixed

Methods

__construct()  : mixed
__get()  : string
Retrieve any attribute of the tag via: attr_ {attribute name} ex: $ tag-> attr_style to get the css style the 'id', 'name' and 'class' attributes can be retrieved directly without using the attr_ prefix For non-standard attributes defined by the user, you must enter data_ instead of attr_ once converted to html, these attributes will be named "data- {attribute name}" to become accessible for JS conterpart thru dataset property.
__set()  : void
Magic method to set attributes of the HTML element.
__toString()  : string
Magic method called when a string operation is done with the current object.
__unset()  : void
Magic method to unset an attribute of the HTML element.
add_after()  : int|bool
Adds an item or a list of items to be rendered after the current HTML element.
add_child()  : int|bool
Adds a child element or a list of child elements to the current HTML element.
add_class()  : void
Adds a CSS class to the HTML element's class attribute.
add_debug()  : mixed
after_html()  : string
Generates the HTML for items that should be rendered after the current element.
close_html()  : string
Generates the closing HTML tag for the current element.
close_tag()  : string
Create a closing tag
content_html()  : string
Generates the HTML content of the element's children.
copy_attributes_to()  : void
Copies attributes, data, and extra properties from the current HTML element to another.
debug_structure()  : Html
exepose the current object structure as an object tree.
del_attribute()  : void
Delete an HTML attribute for the element.
find_child()  : Html|null
Recursively searches for a child element within the current HTML element's children.
full_html()  : string
Generates the complete HTML for the current object/element, including its opening tag, content, and closing tag.
get_children()  : array<string|int, mixed>
All is in the name
get_items_after()  : array<string|int, mixed>
Retrieves the items that should be rendered after the current HTML element.
get_unique_id()  : string
create the unique_id for html tag
is_simple()  : bool
check if the tag is a simple one like <br> with no closing tag
label_tag()  : Html
Create an Html::Label tag from $label and $attributes or from $this->label and $this->attributes of the current object.
open_html_tag()  : string
Generates the opening HTML tag for the current element.
open_tag()  : string
Create an opening tag with its attributes
output()  : string
Will return the current HTML content as string.
parse_event()  : mixed
Retrieve event that need to pass by event.js it automaticaly removed onclick or onchange and over attributes related to events, to pass them thru the event.js class.
reset_key()  : mixed
Reset key_name
set_attribute()  : void
Sets an HTML attribute for the element.
set_extra()  : void
Sets an extra property for the element.
tag()  : Instance
Create a new Html instance for a tag

Constants

_CHECKMARK_BOLD

public mixed _CHECKMARK_BOLD = '&#10004;'

_CHECKMARK_NORMAL

public mixed _CHECKMARK_NORMAL = '&#10003;'

_DOWN_ARROW

public mixed _DOWN_ARROW = '&darr;'

_DOWN_DASHED_ARROW

public mixed _DOWN_DASHED_ARROW = '&#8675;'

_LEFT_ARROW

public mixed _LEFT_ARROW = '&larr;'

_LEFT_DASHED_ARROW

public mixed _LEFT_DASHED_ARROW = '&#8672;'

_RIGHT_ARROW

public mixed _RIGHT_ARROW = '&rarr;'

_RIGHT_ARROW_HEAD

public mixed _RIGHT_ARROW_HEAD = '&#10148;'

_RIGHT_DASHED_ARROW

public mixed _RIGHT_DASHED_ARROW = '&#8674;'

_STAR_EMPTY

public mixed _STAR_EMPTY = '&#9734;'

_STAR_FULL

public mixed _STAR_FULL = '&#9733;'

_UP_ARROW

public mixed _UP_ARROW = '&uarr;'

_UP_DASHED_ARROW

public mixed _UP_DASHED_ARROW = '&#8673;'

_X_BOLD

public mixed _X_BOLD = '&#10006;'

_X_NORMAL

public mixed _X_NORMAL = '&#10005;'

AUDIO

public mixed AUDIO = 'audio'

BODY

public mixed BODY = 'body'

BR

public mixed BR = 'br'

BUTTON

public mixed BUTTON = 'button'

CANVAS

public mixed CANVAS = 'canvas'

DATALIST

public mixed DATALIST = 'datalist'

DETAILS

public mixed DETAILS = 'details'

DIV

public mixed DIV = 'div'

FIELDSET

public mixed FIELDSET = 'fieldset'

FORM

public mixed FORM = 'form'

HEAD

public mixed HEAD = 'head'

HR

public mixed HR = 'hr'

HTML

public mixed HTML = 'html'

IMAGE

public mixed IMAGE = 'IMAGE'

IMG

public mixed IMG = 'img'

INPUT

public mixed INPUT = 'input'

LABEL

public mixed LABEL = 'label'

LEGEND

public mixed LEGEND = 'legend'

LI

public mixed LI = 'li'
public mixed LINK = 'link'

META

public mixed META = 'meta'

OPTGROUP

public mixed OPTGROUP = 'optgroup'

OPTION

public mixed OPTION = 'option'

PARAM

public mixed PARAM = 'param'

QUICK_EDIT_DATA

public mixed QUICK_EDIT_DATA = 'qe_d'

QUICK_EDIT_FIELD

public mixed QUICK_EDIT_FIELD = 'qe_f'

QUICK_EDIT_ID

public mixed QUICK_EDIT_ID = 'qe_i'

QUICK_EDIT_TYPE

public mixed QUICK_EDIT_TYPE = 'qe_t'

QUICK_EDIT_TYPE_INDEX

public mixed QUICK_EDIT_TYPE_INDEX = 'qe_ti'

SCRIPT

public mixed SCRIPT = 'script'

SELECT

public mixed SELECT = 'select'

simple_tags

public mixed simple_tags = [\helPHP\libs\Html::IMG, \helPHP\libs\Html::IMAGE, \helPHP\libs\Html::BR, \helPHP\libs\Html::HR, \helPHP\libs\Html::INPUT, \helPHP\libs\Html::LINK, \helPHP\libs\Html::META, \helPHP\libs\Html::PARAM, \helPHP\libs\Html::USE]

SOURCE

public mixed SOURCE = 'source'

SPAN

public mixed SPAN = 'span'

STYLE

public mixed STYLE = 'style'

SUMMARY

public mixed SUMMARY = 'summary'

SVG

public mixed SVG = 'svg'

TABLE

public mixed TABLE = 'table'

TBODY

public mixed TBODY = 'tbody'

TD

public mixed TD = 'td'

TEXTAREA

public mixed TEXTAREA = 'textarea'

TFOOT

public mixed TFOOT = 'tfoot'

TH

public mixed TH = 'th'

THEAD

public mixed THEAD = 'thead'

TITLE

public mixed TITLE = 'title'

TR

public mixed TR = 'tr'

UL

public mixed UL = 'ul'

USE

public mixed USE = 'use'

VIDEO

public mixed VIDEO = 'video'

Properties

$children

public mixed $children = array()

$current_value

public mixed $current_value = null

$current_value_label

public mixed $current_value_label = null

$debug

public mixed $debug = false

$extra

public mixed $extra = []

$items_after

public mixed $items_after = array()

$label

public mixed $label = ''

$label_for

public mixed $label_for = false

Methods

__construct()

public __construct([mixed $tag_name = '' ][, mixed $attributes = null ][, mixed $content = null ]) : mixed
Parameters
$tag_name : mixed = ''
$attributes : mixed = null
$content : mixed = null

__get()

Retrieve any attribute of the tag via: attr_ {attribute name} ex: $ tag-> attr_style to get the css style the 'id', 'name' and 'class' attributes can be retrieved directly without using the attr_ prefix For non-standard attributes defined by the user, you must enter data_ instead of attr_ once converted to html, these attributes will be named "data- {attribute name}" to become accessible for JS conterpart thru dataset property.

public __get(string $property) : string

ex :

for div equivalent objet as $div : $custom=$div.data_custom; <- will trig a __get

Parameters
$property : string
Return values
string

or null

__set()

Magic method to set attributes of the HTML element.

public __set(string $property, mixed $value) : void
Parameters
$property : string

The name of the property to set. Can be 'id', 'name', 'class', or prefixed with 'attr_', 'data_', 'chld_', or 'next_'.

$value : mixed

The value to set for the property.

__toString()

Magic method called when a string operation is done with the current object.

public __toString() : string

it will act as a shortcut to full_html().

Return values
string

A string representation of the current HTML element.

__unset()

Magic method to unset an attribute of the HTML element.

public __unset(string $property) : void
Parameters
$property : string

The name of the property to unset. Can be 'id', 'name', 'class', or prefixed with 'attr_', 'data_', 'chld_', or 'next_'.

add_after()

Adds an item or a list of items to be rendered after the current HTML element.

public add_after(mixed $itemList[, string $key_name = '' ]) : int|bool
Parameters
$itemList : mixed

The item(s) to add. Can be a single Html object, a string, or an array of Html objects or strings.

$key_name : string = ''

Optional key name for the item if it's a single item and needs a specific key.

Return values
int|bool

Returns the index of the last added item if successful, or false if the item list is invalid.

add_child()

Adds a child element or a list of child elements to the current HTML element.

public add_child(mixed $child_list[, string $key_name = '' ]) : int|bool
Parameters
$child_list : mixed

The child element(s) to add. Can be a single Html object, a string, or an array of Html objects or strings.

$key_name : string = ''

Optional key name for the child element if it's a single element and needs a specific key.

Return values
int|bool

Returns the index of the last added child if successful, or false if the element is simple or the child list is invalid. If a key is provided the child is added by the given key and the function return the index of the added element.

add_class()

Adds a CSS class to the HTML element's class attribute.

public add_class(string $value) : void
Parameters
$value : string

The CSS class to add.

add_debug()

public add_debug(mixed $item) : mixed
Parameters
$item : mixed

after_html()

Generates the HTML for items that should be rendered after the current element.

public after_html([int $l = 0 ]) : string

This method iterates through the items in the items_after array. If an item is an Html object, it recursively calls its full_html method. Otherwise, it treats the item as a string.

Parameters
$l : int = 0

The current indentation level (for pretty printing).

Return values
string

The HTML content of the items to be rendered after.

close_html()

Generates the closing HTML tag for the current element.

public close_html() : string
Return values
string

The closing HTML tag.

close_tag()

Create a closing tag

public static close_tag(mixed $tag_name) : string
Parameters
$tag_name : mixed
Return values
string

content_html()

Generates the HTML content of the element's children.

public content_html([int $l = 0 ]) : string

This method iterates through the children of the current HTML element. If a child is an Html object, it recursively calls its full_html method. Otherwise, it treats the child as a string.

Parameters
$l : int = 0

The current indentation level (for pretty printing).

Return values
string

The HTML content of the children.

copy_attributes_to()

Copies attributes, data, and extra properties from the current HTML element to another.

public copy_attributes_to(Html $destination_tag) : void
Parameters
$destination_tag : Html

The target HTML element to copy attributes to.

debug_structure()

exepose the current object structure as an object tree.

public debug_structure([string $key_name = '' ][, string $parent = '' ][, int $l = 0 ][, bool $is_next = false ]) : Html

Helpfull to debug very complexe object tree.

Parameters
$key_name : string = ''

the key to use to identify the current object in the tree

$parent : string = ''

the parent object to use as a reference

$l : int = 0

the level of the current object in the tree and to force usage of $key_name and $parent

$is_next : bool = false

to start with next item or first child.

Return values
Html

structure of the objet as an Html tree.

del_attribute()

Delete an HTML attribute for the element.

public del_attribute(string $name) : void

can be useful for boolean attributes, like "controls" for video

Parameters
$name : string

The name of the attribute.

find_child()

Recursively searches for a child element within the current HTML element's children.

public find_child(string $key_name[, int $maxRecursion = 0 ][, Html|null $parent = null ][, int $level = 0 ]) : Html|null
Parameters
$key_name : string

The key name of the child element to find.

$maxRecursion : int = 0

The maximum recursion depth for the search. Default is 0 (only direct children are searched).

$parent : Html|null = null

The parent element to start the search from. Defaults to the current element.

$level : int = 0

The current recursion level.

Return values
Html|null

Returns the found child element as an Html object, or null if not found. If the key name is found in the direct children, it returns that child.

full_html()

Generates the complete HTML for the current object/element, including its opening tag, content, and closing tag.

public full_html([int $l = 0 ]) : string

This method orchestrates the generation of the full HTML representation of the element. It handles both standard HTML tags and tag-less groups of children. For standard tags, it calls open_html_tag, content_html, and close_html. For simple tags (like
), it omits the content and closing tag. It also appends any HTML content specified to be rendered after the element.

Parameters
$l : int = 0

The current indentation level for pretty-printing the HTML.

Return values
string

The complete HTML string for the element.

get_children()

All is in the name

public get_children() : array<string|int, mixed>
Return values
array<string|int, mixed>

Returns an array containing the child elements of the current HTML element.

get_items_after()

Retrieves the items that should be rendered after the current HTML element.

public get_items_after() : array<string|int, mixed>
Return values
array<string|int, mixed>

Returns an array containing the item to be rendered after.

get_unique_id()

create the unique_id for html tag

public static get_unique_id() : string
Return values
string

is_simple()

check if the tag is a simple one like <br> with no closing tag

public static is_simple(mixed $tag_name) : bool
Parameters
$tag_name : mixed
Return values
bool

true false

label_tag()

Create an Html::Label tag from $label and $attributes or from $this->label and $this->attributes of the current object.

public label_tag([string $label = null ][, array<string|int, mixed> $attributes = null ]) : Html
Parameters
$label : string = null
$attributes : array<string|int, mixed> = null
Return values
Html

object Html::label

open_html_tag()

Generates the opening HTML tag for the current element.

public open_html_tag() : string

This includes the tag name and all its attributes (standard and data-*). It also handles special cases like the DOCTYPE for the tag and event parsing for JavaScript integration. For quick edit fields/forms, it encrypt the field infos to avoid XSS or SQL injection.

Return values
string

The opening HTML tag as a string.

open_tag()

Create an opening tag with its attributes

public static open_tag(mixed $tag_name[, null $attributes = null ]) : string
Parameters
$tag_name : mixed
$attributes : null = null
Return values
string

output()

Will return the current HTML content as string.

public static output(mixed $htmlContent) : string

Echo force the call to magic method __toString that will convert everything with full_html()

Parameters
$htmlContent : mixed
Return values
string

html

parse_event()

Retrieve event that need to pass by event.js it automaticaly removed onclick or onchange and over attributes related to events, to pass them thru the event.js class.

public parse_event() : mixed

This will avoid conflict and permit multiple similar events on the same tag Except for A tag because they need to stop the original event to not trig the page reload This function is called automaticaly

reset_key()

Reset key_name

public reset_key([mixed $newValue = '' ]) : mixed
Parameters
$newValue : mixed = ''

set_attribute()

Sets an HTML attribute for the element.

public set_attribute(string $name, mixed $value) : void
Parameters
$name : string

The name of the attribute.

$value : mixed

The value of the attribute.

set_extra()

Sets an extra property for the element.

public set_extra(string $name, mixed $value) : void
Parameters
$name : string

The name of the extra property.

$value : mixed

The value of the extra property.

tag()

Create a new Html instance for a tag

public static tag([string $tag_name = '' ][, null $attributes = null ][, null $content = null ]) : Instance
Parameters
$tag_name : string = ''
$attributes : null = null
$content : null = null
Return values
Instance

object


        
On this page

Search results