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  
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

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