PHP Classes

How to Use a PHP HTML Class to Compose The Contents of a Web Page Using Objects From the Package Hypertool: Compose and generate HTML pages from objects

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-04-13 (4 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
hypertool 2.0.0MIT/X Consortium ...7.4HTML
Description 

Author

This package can compose library provides a powerful, flexible, and generate HTML pages from objects.

It provides classes that have the properties of different HTML tags.

Applications can compose the HTML page structure, creating HTML tag objects fluent PHP interface for generating HTML, with first-class support for modern frontend techniques using HTMX and associating parent page objects with child page objects.

The class can generate HTML for the whole document after the page is composed.

Currently, the package supports the tags:

- a
- abbr
- address
- area
- article
- aside
- audio
- base
- bdi
- bdo
- blockquote
- body
- br
- button
- canvas
- caption
- cite
- code
- col
- colgroup
- data
- datalist
- dd
- del
- details
- dfn
- dialog
- div
- dl
- dt
- em
- embed
- fieldset
- figcaption
- figure
- footer
- form
- h
- head
- header
- hr
- i
- iframe
- img
- input
- ins
- kbd
- label
- legend
- li
- link
- main
- map
- mark
- meta
- meter
- nav
- noscript
- object
- ol
- optgroup
- option
- output
- p
- param
- picture

Picture of Juan Camacho
  Performance   Level  
Name: Juan Camacho <contact>
Classes: 8 packages by
Country: Spain Spain
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Instructions

This library provides a powerful, flexible, and fluent PHP interface for generating HTML, with first-class support for modern frontend techniques using HTMX and Hyperscript.

Please read this document's package usage instructions.

Documentation

Hypertool HTML Generator - Documentation

License: MIT <!-- Adjust path if LICENSE is in root --> HTMX Hyperscript GitHub Repo <!-- Update link -->

Welcome to the official documentation for the Hypertool HTML Generator library!

This library provides a powerful, flexible, and fluent PHP interface for generating HTML, with first-class support for modern frontend techniques using HTMX and Hyperscript.

? Getting Started

New to the library? Here's how to get up and running quickly:

  1. Installation: Follow the Composer installation instructions in the main project README.
  2. Basic Usage: Check out the basic usage examples to see how to create elements.
  3. Framework Integration: See examples for HTMX & Hyperscript and the Automated Script Injection system.
  4. Run Examples: Explore the runnable code examples in the ../examples/ directory.

? Core Class Documentation

Dive deeper into the main components of the library:

  • HtmlElement: The core class for creating and manipulating all HTML elements. Learn about its API, attribute setters, child management, and HTMX/Hyperscript integration.
  • ScriptManager: Understand how to use the automated system for injecting `htmx.js` and `hyperscript.js` conditionally and efficiently into your layouts.

? Key Features

  • ? Full HTML Coverage: Classes and factories for all HTML5 & deprecated elements.
  • ? Fluent Interface: Chain methods for cleaner code (`->setId()->setClass()->...`).
  • ? HTMX & Hyperscript Ready: Dedicated setters for `hx-*` and `_` attributes.
  • ?? Smart Script Loading: Automated, conditional, environment-aware script injection via `ScriptManager`.
  • ? Composer Ready: Easily integrate into your projects with PSR-4 autoloading.
  • ? Well Documented: Comprehensive class documentation and usage examples.

? Contributing

Found a bug or have an idea? We welcome contributions!

? Useful Links

License: MIT <!-- Adjust path if LICENSE is in root --> HTMX Hyperscript GitHub Repo <!-- Update link -->

Welcome to the official documentation for the Hypertool HTML Generator library!

This library provides a powerful, flexible, and fluent PHP interface for generating HTML, with first-class support for modern frontend techniques using HTMX and Hyperscript.

? Getting Started

New to the library? Here's how to get up and running quickly:

  1. Installation: Follow the Composer installation instructions in the main project README.
  2. Basic Usage: Check out the basic usage examples to see how to create elements.
  3. Framework Integration: See examples for HTMX & Hyperscript and the Automated Script Injection system.
  4. Run Examples: Explore the runnable code examples in the ../examples/ directory.

? Core Class Documentation

Dive deeper into the main components of the library:

  • HtmlElement: The core class for creating and manipulating all HTML elements. Learn about its API, attribute setters, child management, and HTMX/Hyperscript integration.
  • ScriptManager: Understand how to use the automated system for injecting `htmx.js` and `hyperscript.js` conditionally and efficiently into your layouts.

? Key Features

  • ? Full HTML Coverage: Classes and factories for all HTML5 & deprecated elements.
  • ? Fluent Interface: Chain methods for cleaner code (`->setId()->setClass()->...`).
  • ? HTMX & Hyperscript Ready: Dedicated setters for `hx-*` and `_` attributes.
  • ?? Smart Script Loading: Automated, conditional, environment-aware script injection via `ScriptManager`.
  • ? Composer Ready: Easily integrate into your projects with PSR-4 autoloading.
  • ? Well Documented: Comprehensive class documentation and usage examples.

? Contributing

Found a bug or have an idea? We welcome contributions!

? Useful Links


Details

HTML Element Generator

License: MIT HTMX Hyperscript

Overview

This PHP library provides a comprehensive, extensible, and fluent interface for generating HTML elements, including full support for all HTML5 and deprecated elements. It features:

  • Dedicated PHP classes and static factory methods for every HTML element
  • Fluent attribute and child management
  • Comprehensive support for HTMX and Hyperscript
  • Automated, environment-aware script injection with robust error handling
  • Usage examples and documentation in every element class file

Table of Contents

Features

  • All HTML5 and deprecated elements as PHP classes
  • Static factory methods for all elements via `HtmlElement::elementname()`
  • Fluent interface for setting attributes and adding children
  • Usage examples in every element class file
  • HTMX and Hyperscript support with dedicated setters
  • Automated, environment-aware script injection (see ScriptManager)
  • Easily extensible for future HTML elements and frontend libraries

Setup

  1. Install via Composer:
    composer require hypertool/html
    
  2. Include the Composer autoloader in your project:
    require_once 'vendor/autoload.php';
    
  3. Use the namespaced classes:
    use Hypertool\Html\HtmlElement;
    use Hypertool\Html\ScriptManager;
    // ... etc.
    
  4. (Optional) Configure environment: - Set the `APP_ENV` environment variable to `production` for local asset loading, or leave unset for CDN loading.

Usage

Basic Usage

// Assumes autoloader is included as per Setup instructions
use Hypertool\Html\HtmlElement;
use Hypertool\Html\H1; // Example specific element

// Using the generic HtmlElement class
$html = new HtmlElement('div');
$html->setId('myDiv')->setClass('some-class');
$html->add_child('MainTitle','h1')->text('Hello, World!')->setClass('title');
$html->MainTitle->setStyle('color: blue;');
echo $html->output();

Using Dedicated Element Classes

// Assumes autoloader is included and 'use Hypertool\Html\Div;' is present
$div = new Div('Content inside div');
echo $div->output();

Using Static Factory Methods

// Assumes autoloader is included and 'use Hypertool\Html\HtmlElement;' is present
$div = HtmlElement::div('Content inside div');
echo $div->output();

Usage Examples in Element Files

Each element class file (e.g., src/b.php, src/section.php) contains a usage example at the end, demonstrating both direct instantiation and static factory usage.

HTMX & Hyperscript

This library provides comprehensive support for HTMX and Hyperscript:

  • Dedicated setters for all official HTMX attributes and events (e.g., `setHxGet`, `setHxPost`, `setHxTrigger`, `setHxSwap`, etc.)
  • Dedicated setter for Hyperscript's _ attribute (`setHyperscript`)
  • HTMX event support via `setHxOn($event, $handler)`

HTMX Usage Example

// Assumes autoloader is included and 'use Hypertool\Html\HtmlElement;' is present
$button = HtmlElement::button('Load Data')
    ->setHxGet('/api/data')
    ->setHxTarget('#result')
    ->setHxSwap('outerHTML');
echo $button->output();

Hyperscript Usage Example

// Assumes autoloader is included and 'use Hypertool\Html\HtmlElement;' is present
$button = HtmlElement::button('Click Me')
    ->setHyperscript('on click add .clicked to me');
echo $button->output();

Combining HTMX and Hyperscript

// Assumes autoloader is included and 'use Hypertool\Html\HtmlElement;' is present
$button = HtmlElement::button('Load & Animate')
    ->setHxGet('/api/data')
    ->setHyperscript('on htmx:afterSwap add .animated to #result');
echo $button->output();

Automated Script Injection

Use the ScriptManager class (now namespaced) for robust, environment-aware script loading:

// Assumes autoloader is included and 'use Hypertool\Html\ScriptManager;' is present

// In your page/component:
ScriptManager::requireHtmx();
ScriptManager::requireHyperscript();

// In your layout/footer (once per page):
echo ScriptManager::outputScripts();

  • Loads from CDN in development, local assets in production
  • Prevents duplicate inclusions
  • Adds error handling and a <noscript> fallback

Coverage

  • All HTML5 elements and most deprecated elements are included.
  • Deprecated elements (e.g., `<marquee>`, `<font>`, `<center>`, etc.) are supported for legacy compatibility.
  • For a full list of supported elements, see `src/html_elements_master_list.php`.

Contribution Guidelines

We welcome contributions! To contribute:

  1. Fork the repository and create a new branch.
  2. Add or update code, tests, or documentation.
  3. Ensure your code follows the project's style and passes any tests.
  4. Submit a pull request with a clear description of your changes.

To add new HTML elements or attributes: - Create a new PHP class in src/ for the element. - Add usage examples at the end of the file. - If adding new HTMX/Hyperscript features, update the relevant setters and documentation.

To report bugs or request features: - Open an issue on GitHub. // Updated link

License

This project is licensed under the MIT License.

Links


  Files folder image Files (159)  
File Role Description
Files folder image.github (1 directory)
Files folder image.vscode (1 file)
Files folder imagedocs (3 files)
Files folder imageexamples (4 files)
Files folder imagescripts (1 file)
Files folder imagesrc (144 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file plan_composer_support.md Data Auxiliary data
Accessible without login Plain text file plan_expand_html_class.md Data Auxiliary data
Accessible without login Plain text file plan_htmx_support.md Data Auxiliary data
Accessible without login Plain text file Readme.md Doc. Documentation

  Files folder image Files (159)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (159)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file php.yml Data Auxiliary data

  Files folder image Files (159)  /  .vscode  
File Role Description
  Accessible without login Plain text file settings.json Data Auxiliary data

  Files folder image Files (159)  /  docs  
File Role Description
  Accessible without login Plain text file HtmlElement.md Example Example script
  Accessible without login Plain text file README.md Doc. Documentation
  Accessible without login Plain text file ScriptManager.md Data Auxiliary data

  Files folder image Files (159)  /  examples  
File Role Description
  Accessible without login Plain text file bootstrap_example.php Example Example script
  Accessible without login Plain text file html_element_example.php Example Example script
  Plain text file script_manager_example.php Class Class source
  Accessible without login Plain text file tailwind_example.php Example Example script

  Files folder image Files (159)  /  scripts  
File Role Description
  Accessible without login Plain text file add_namespace.php Aux. Configuration script

  Files folder image Files (159)  /  src  
File Role Description
  Plain text file a.php Class Class source
  Plain text file abbr.php Class Class source
  Plain text file acronym.php Class Class source
  Plain text file address.php Class Class source
  Accessible without login Plain text file add_usage_examples.php Aux. Configuration script
  Plain text file applet.php Class Class source
  Plain text file area.php Class Class source
  Plain text file article.php Class Class source
  Plain text file aside.php Class Class source
  Plain text file audio.php Class Class source
  Plain text file b.php Class Class source
  Plain text file base.php Class Class source
  Plain text file basefont.php Class Class source
  Plain text file bdi.php Class Class source
  Plain text file bdo.php Class Class source
  Plain text file bgsound.php Class Class source
  Plain text file big.php Class Class source
  Plain text file blink.php Class Class source
  Plain text file blockquote.php Class Class source
  Plain text file body.php Class Class source
  Plain text file br.php Class Class source
  Plain text file button.php Class Class source
  Plain text file canvas.php Class Class source
  Plain text file caption.php Class Class source
  Plain text file center.php Class Class source
  Accessible without login Plain text file check_missing_elements.php Aux. Configuration script
  Plain text file cite.php Class Class source
  Plain text file code.php Class Class source
  Plain text file col.php Class Class source
  Plain text file colgroup.php Class Class source
  Plain text file command.php Class Class source
  Plain text file content.php Class Class source
  Plain text file data.php Class Class source
  Plain text file datalist.php Class Class source
  Plain text file dd.php Class Class source
  Plain text file del.php Class Class source
  Plain text file details.php Class Class source
  Plain text file dfn.php Class Class source
  Plain text file dialog.php Class Class source
  Plain text file dir.php Class Class source
  Plain text file div.php Class Class source
  Plain text file dl.php Class Class source
  Plain text file dt.php Class Class source
  Plain text file element.php Class Class source
  Plain text file em.php Class Class source
  Plain text file embed.php Class Class source
  Plain text file fieldset.php Class Class source
  Plain text file figcaption.php Class Class source
  Plain text file figure.php Class Class source
  Plain text file font.php Class Class source
  Plain text file footer.php Class Class source
  Plain text file form.php Class Class source
  Plain text file frame.php Class Class source
  Plain text file frameset.php Class Class source
  Accessible without login Plain text file generate_missing_elements.php Aux. Configuration script
  Plain text file h.php Class Class source
  Plain text file head.php Class Class source
  Plain text file header.php Class Class source
  Plain text file hr.php Class Class source
  Plain text file html.php Class Class source
  Plain text file Html.php Class Class source
  Accessible without login Plain text file html_elements_master_list.php Aux. Configuration script
  Plain text file i.php Class Class source
  Plain text file iframe.php Class Class source
  Plain text file image.php Class Class source
  Plain text file img.php Class Class source
  Plain text file input.php Class Class source
  Plain text file ins.php Class Class source
  Plain text file isindex.php Class Class source
  Plain text file kbd.php Class Class source
  Plain text file keygen.php Class Class source
  Plain text file label.php Class Class source
  Plain text file legend.php Class Class source
  Plain text file li.php Class Class source
  Plain text file link.php Class Class source
  Plain text file listing.php Class Class source
  Plain text file main.php Class Class source
  Plain text file map.php Class Class source
  Plain text file mark.php Class Class source
  Plain text file marquee.php Class Class source
  Plain text file menuitem.php Class Class source
  Plain text file meta.php Class Class source
  Plain text file meter.php Class Class source
  Plain text file multicol.php Class Class source
  Plain text file nav.php Class Class source
  Plain text file nextid.php Class Class source
  Plain text file nobr.php Class Class source
  Plain text file noembed.php Class Class source
  Plain text file noframes.php Class Class source
  Plain text file noscript.php Class Class source
  Plain text file object.php Class Class source
  Plain text file ol.php Class Class source
  Plain text file optgroup.php Class Class source
  Plain text file option.php Class Class source
  Plain text file output.php Class Class source
  Plain text file p.php Class Class source
  Plain text file param.php Class Class source
  Plain text file picture.php Class Class source
  Plain text file plaintext.php Class Class source
  Plain text file pre.php Class Class source
  Plain text file progress.php Class Class source
  Plain text file q.php Class Class source
  Plain text file rb.php Class Class source
  Plain text file rp.php Class Class source
  Plain text file rt.php Class Class source
  Plain text file rtc.php Class Class source
  Plain text file ruby.php Class Class source
  Plain text file s.php Class Class source
  Plain text file samp.php Class Class source
  Plain text file script.php Class Class source
  Plain text file ScriptIncludes.php Class Class source
  Plain text file ScriptManager.php Class Class source
  Plain text file section.php Class Class source
  Plain text file select.php Class Class source
  Plain text file slot.php Class Class source
  Plain text file small.php Class Class source
  Plain text file source.php Class Class source
  Plain text file spacer.php Class Class source
  Plain text file span.php Class Class source
  Plain text file strike.php Class Class source
  Plain text file strong.php Class Class source
  Plain text file style.php Class Class source
  Plain text file sub.php Class Class source
  Plain text file summary.php Class Class source
  Plain text file sup.php Class Class source
  Plain text file table.php Class Class source
  Plain text file tbody.php Class Class source
  Plain text file td.php Class Class source
  Plain text file template.php Class Class source
  Plain text file textarea.php Class Class source
  Plain text file tfoot.php Class Class source
  Plain text file th.php Class Class source
  Plain text file thead.php Class Class source
  Plain text file time.php Class Class source
  Plain text file title.php Class Class source
  Plain text file tr.php Class Class source
  Plain text file track.php Class Class source
  Plain text file tt.php Class Class source
  Plain text file u.php Class Class source
  Plain text file ul.php Class Class source
  Plain text file var.php Class Class source
  Plain text file video.php Class Class source
  Plain text file wbr.php Class Class source
  Plain text file xmp.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0