joas8211/html-composition

HTML builder for PHP

1.0.3 2020-11-16 19:48 UTC

This package is auto-updated.

Last update: 2025-07-17 06:17:28 UTC


README

HTML builder for PHP

Features

  • Method chaining 🔗
  • Optional pretty printing 😍
  • Element or complete document rendering 🎨
  • Configurable indentation 📝
  • Code injection 💉

Installation

With Composer

Install HTML Composition to your project with Composer:

composer require joas8211/html-composition

And require autoload.php if you haven't already:

require __DIR__ . '/vendor/autoload.php';

Without Composer

  1. Download HtmlComposition.php from releases.
  2. Require the file in the PHP file where you need it.
require 'HtmlComposition.php';

Usage

Example usage:

use HtmlComposition\HtmlComposition;

echo (new HtmlComposition)
    ->document()
    ->tag('html', ['lang' => 'en'])
        ->tag('head')
            ->tag('title')->text('Example Document')->end()
        ->end()
        ->tag('body')
            ->tag('h1')->text('Hello World!')->end()
            ->tag('img', [
                'src' => 'https://picsum.photos/768/432',
                'alt' => '',
            ], true)
        ->end()
    ->end();

Above code generates following HTML:

<!doctype html>
<html lang="en">
    <head>
        <title>
            Example Document
        </title>
    </head>
    <body>
        <h1>
            Hello World!
        </h1>
        <img src="https://picsum.photos/768/432" alt="">
    </body>
</html>