Download the PHP package one234ru/html-tag-generator without Composer
On this page you can find all versions of the php package one234ru/html-tag-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download one234ru/html-tag-generator
More information about one234ru/html-tag-generator
Files in one234ru/html-tag-generator
Package html-tag-generator
Short Description PHP tool for generating code of HTML tags.
License GPL-3.0-or-later
Informations about the package html-tag-generator
ПО-РУССКИ
HTML tags source code generation based on configuration array
Installation
Composer
Manual
The library doesn't have any dependencies. Only HTMLtagGenerator.php file is required for work.
Usage
Create the object passing configuration array to the constructor. Then get an HTML code treating the object as a string:
Description of configuration fields
tag
and text
tag
is the tag's name, div
by default.
text
will be inserted right after opening tag.
Examples
Configuration:
Result:
Configuration:
Result:
Tags with no closing part (like input
) will be detected automatically and processed
appropriately.
attr
attr
is a list of attributes in a form of key-value pairs.
All the values are encoded using htmlspecialchars().
If the value is an array, it will be turned into JSON.
Result (formatted for readability):
Some attributes, like checked
, are treated in a special way:
if the value converts to boolean true
, only their names go to final HTML,
otherwise nothing goes anywhere:
This is done for disambiguation of unobvious feature deriving from HTML standard, which
leads to <input checked="">
or
<input checked=0>
eventually working the same way as <input checked>
, i.e. the actual
value of the attribute not affecting anything even if it is false
or something like that,
and the only way to discard the attribute is to exclude it completely.
children
The children
field serves for listing of children elements in the form of similar
configurations.
Their source code will be inserted after text
:
You can pass HTML as a string directly instead of an array:
It is not necessary to wrap the contents into tags:
Actually, the following two configurations yield the same result:
If a string is passed as a configuration, it will be used as final HTML;
this fact is utilized when children
are passed as strings. The following two configurations
are equivalent:
Result:
Inheritance: normalizing configuration (normalizeConfig()
)
When extending the class, you may define protected normalizeConfig()
method,
which allows using configurations of non-standard structure.
Let's say, we often use class
attribute and would like to define it at the highest level of
array, not inside the attr
. In this case we need to create a child class:
Result: