2017 © Pedro Peláez
 

library html-table-builder

An HTML Table Builder

image

tlr/html-table-builder

An HTML Table Builder

  • Wednesday, December 7, 2016
  • by tedslittlerobot
  • Repository
  • 0 Watchers
  • 2 Stars
  • 1,104 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 6 Versions
  • 75 % Grown

The README.md

HTML Table Builder

Build Status, (*1)

A class-based, "fluent" HTML table builder in PHP., (*2)

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('<a href="/create">Add a new entry</a>')->dontEscape()->span(3);

echo $table->render();

Installation

composer require tlr/html-table-builder

Basic Usage

You can see much of the functionality in the above example. More detailed explanations are below., (*3)

Reference

Table

The table object represents a top level HTML table., (*4)

See Classable below for information on setting the rows' classes., (*5)

See Attributable below for information on setting other attributes on the cell element., (*6)

It has four other main methods., (*7)

$table->render()

This is a helpful shortcut to doing the following:, (*8)

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

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

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

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

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

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

You can call them multiple times, and they will return the same object., (*12)

Section (thead, tbody, tfoot)

These objects represent sections in a table., (*13)

See Classable below for information on setting the rows' classes., (*14)

See Attributable below for information on setting other attributes on the cell element., (*15)

There are two other methods to talk about here., (*16)

$section->row()

This initialises and returns a new Row object. Calling it multiple times will add multiple rows to the table., (*17)

$section->nextRow(Row $current)

Returns the next row in its children from the one passed to it., (*18)

Throws an error if passed a row that is not one of its children., (*19)

See the code example for the logic of how this works., (*20)


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

Row

These objects represent rows in a table section., (*21)

See Classable below for information on setting the rows' classes., (*22)

See Attributable below for information on setting other attributes on the cell element., (*23)

$row->addCell(CellInterface $cell)

This adds a new cell to the row. You can use this to add a custom cell to the row., (*24)

The following helper methods use this method to add the basic included cell types., (*25)

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

Cells

These objects represent cells in a table row., (*26)

See Spannable below for information on setting the column spanning., (*27)

See Classable below for information on setting the cells' classes., (*28)

See Attributable below for information on setting other attributes on the cell element., (*29)

Content Cell

This is the default cell. It is fairly versatile, although basic., (*30)

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

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

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., (*33)

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

See below for some examples., (*35)

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

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

$row->linkCell('https://google.com')->content('Visit Google.');
// <td><a href="https://google.com">Visit Google.</a></td>

$cell = new LinkCell('localhost')->content('

some preformatted text
')->raw();

Image Cell

$row->imageCell('cat.jpg');
// <td><img src="cat.jpg" /></td>

Traits

Spannable

On the Cell objects, you can call:, (*37)

// 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., (*38)

Classable

On any of the element objects - Table, Section, Row, Cell, you can set the class. The following three examples all result in <table class="ui table">:, (*39)

// 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., (*40)

Attributable

You can set any other HTML attribute:, (*41)

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

This returns the element object for chaining., (*42)

The Versions

07/12 2016

dev-feature/blueprint

dev-feature/blueprint

An HTML Table Builder

  Sources   Download

MIT

The Requires

 

The Development Requires

by Stef Horner

07/12 2016

dev-master

9999999-dev

An HTML Table Builder

  Sources   Download

MIT

The Development Requires

by Stef Horner

07/12 2016

v1.1.0

1.1.0.0

An HTML Table Builder

  Sources   Download

MIT

The Development Requires

by Stef Horner

07/12 2016

dev-feature/table-builder

dev-feature/table-builder

An HTML Table Builder

  Sources   Download

MIT

The Requires

 

The Development Requires

by Stef Horner

28/11 2016

v1.0.1

1.0.1.0

An HTML Table Builder

  Sources   Download

MIT

The Development Requires

by Stef Horner

25/11 2016

v1.0.0

1.0.0.0

An HTML Table Builder

  Sources   Download

MIT

The Requires

 

The Development Requires

by Stef Horner