PHPackages                             tlr/html-table-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tlr/html-table-builder

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tlr/html-table-builder
======================

An HTML Table Builder

v1.1.0(9y ago)1040.1k1[2 issues](https://github.com/tedslittlerobot/html-table-builder/issues)MITPHP

Since Nov 25Pushed 9y ago1 watchersCompare

[ Source](https://github.com/tedslittlerobot/html-table-builder)[ Packagist](https://packagist.org/packages/tlr/html-table-builder)[ RSS](/packages/tlr-html-table-builder/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (5)Used By (0)

HTML Table Builder
==================

[](#html-table-builder)

[![Build Status](https://camo.githubusercontent.com/2c06f6c0e5914403d1cc4ed16ca49d183758bd010fda009792222dd9bbc0238d/68747470733a2f2f7472617669732d63692e6f72672f746564736c6974746c65726f626f742f68746d6c2d7461626c652d6275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/tedslittlerobot/html-table-builder)

A class-based, "fluent" HTML table builder in PHP.

```
use Tlr\Tables\Elements\Table;

$table = new Table;

$table->class('ui celled table');

$row = $table->header()->row();
$row->cell('Name');
$row->cell('Age');
$row->cell('Something Else');

$row = $table->body()->row();

$row->cell('George');
$row->cell('12');
$row->cell('Food');

$table->footer()->row()
    ->cell('Add a new entry')->dontEscape()->span(3);

echo $table->render();
```

Installation
------------

[](#installation)

```
composer require tlr/html-table-builder
```

Basic Usage
-----------

[](#basic-usage)

You can see much of the functionality in the above example. More detailed explanations are below.

Reference
---------

[](#reference)

### Table

[](#table)

The table object represents a top level HTML table.

See `Classable` below for information on setting the rows' classes.

See `Attributable` below for information on setting other attributes on the cell element.

It has four other main methods.

#### `$table->render()`

[](#table-render)

This is a helpful shortcut to doing the following:

```
(new TableRenderer)->renderElement($table);
```

If you want to pretty print the HTML in your table, you can set it like so:

```
(new TableRenderer)->prettyPrint()->renderElement($table);
```

#### `$table->header()` `$table->body()` `$table->footer()`

[](#table-header-table-body-table-footer)

These all return a table section object (see below).

These will be initialised the first time you call the function, so if you never call `->header()`, then a `` section will not be rendered.

You can call them multiple times, and they will return the same object.

### Section (`thead`, `tbody`, `tfoot`)

[](#section-thead-tbody-tfoot)

These objects represent sections in a table.

See `Classable` below for information on setting the rows' classes.

See `Attributable` below for information on setting other attributes on the cell element.

There are two other methods to talk about here.

#### `$section->row()`

[](#section-row)

This initialises and returns a new `Row` object. Calling it multiple times will add multiple rows to the table.

#### `$section->nextRow(Row $current)`

[](#section-nextrowrow-current)

Returns the next row in its children from the one passed to it.

Throws an error if passed a row that is not one of its children.

See the code example for the logic of how this works.

```
$one = $section->row();
$two = $section->row();

$section->nextRow($one) === $two; // true
$section->nextRow($two); // a new row
```

### Row

[](#row)

These objects represent rows in a table section.

See `Classable` below for information on setting the rows' classes.

See `Attributable` below for information on setting other attributes on the cell element.

#### `$row->addCell(CellInterface $cell)`

[](#row-addcellcellinterface-cell)

This adds a new cell to the row. You can use this to add a custom cell to the row.

The following helper methods use this method to add the basic included cell types.

- `$row->cell(string $content = null)`
- `$row->linkCell(string $link)`
- `$row->imageCell(string $src)`

### Cells

[](#cells)

These objects represent cells in a table row.

See `Spannable` below for information on setting the column spanning.

See `Classable` below for information on setting the cells' classes.

See `Attributable` below for information on setting other attributes on the cell element.

#### Content Cell

[](#content-cell)

This is the default cell. It is fairly versatile, although basic.

You can set the content on initialiastaion, or by calling the `->content(string $content)` method.

The default behaviour is to escape the content. If you want to override this, chain in the `->dontEscape()` or `->raw()` methods.

There is a `->wrapContent(string $open, string $content, string $close)` method. This lets you enter some unescaped HTML that wrap some escaped content. Essentially, all this does is escape the `$content` value, then concatenate them in order, and set the cell to `->raw()` or `->dontEscape()`. Nevertheless, it can still be helpful - allowing you to mix your own unescaped HTML with some content that should be escaped.

As with any unescaped content, it is up to you to ensure the HTML is valid.

See below for some examples.

```
$row->cell('Cat'); // Cat
$row->cell(''); // &lt;Cat&gt;
$row->cell('')->raw(); //
$row->cell()->content('Cat'); // Cat
$row->cell()->wrapContent('', '', ''); // &lt;Cat&gt;
```

#### Link Cell

[](#link-cell)

A link cell is a cell with a link in it. Other than the link argument to its constructor, it behaves like a `ContentCell`

```
$row->linkCell('https://google.com')->content('Visit Google.');
// Visit Google.

$cell = new LinkCell('localhost')->content('some preformatted text')->raw();
```

#### Image Cell

[](#image-cell)

```
$row->imageCell('cat.jpg');
//
```

### Traits

[](#traits)

#### Spannable

[](#spannable)

On the `Cell` objects, you can call:

```
// setting rowspan
$row->cell('A double height column')->spanRows(2);

// setting colspan
$row->cell('A triple width column')->spanColumns(3);
```

This returns the element object for chaining.

#### Classable

[](#classable)

On any of the element objects - `Table`, `Section`, `Row`, `Cell`, you can set the class. The following three examples all result in ``:

```
// This is the semantic ui CSS framework table class
$table->class('ui table');

// You don't have to set the classes at the same time.
$table->class('ui')->class('table');

// You can pass an array of string
$table->class(['ui', 'table']);
```

This returns the element object for chaining.

#### Attributable

[](#attributable)

You can set any other HTML attribute:

```
$table->attribute('foo', 'bar');
```

This returns the element object for chaining.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

3

Last Release

3449d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/792e2c24d2cd9ca5c55787efcedc0035632fbd3f7df82fa2b6eaa7f2287e226c?d=identicon)[tedslittlerobot](/maintainers/tedslittlerobot)

---

Top Contributors

[![tedslittlerobot](https://avatars.githubusercontent.com/u/1783459?v=4)](https://github.com/tedslittlerobot "tedslittlerobot (22 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tlr-html-table-builder/health.svg)

```
[![Health](https://phpackages.com/badges/tlr-html-table-builder/health.svg)](https://phpackages.com/packages/tlr-html-table-builder)
```

###  Alternatives

[vipsoft/unzip

Unzip library - a ZipArchive wrapper

331.4M](/packages/vipsoft-unzip)[tomasnorre/crawler

Crawler extension for TYPO3

58397.5k1](/packages/tomasnorre-crawler)[iron-io/iron_core

Collection of common functions for all iron.io client libraries

191.9M4](/packages/iron-io-iron-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
