Skip to content
Bertie2011 edited this page Aug 29, 2021 · 1 revision

CLIFramework comes with a powerful table generator out of the box, which is inspired by Symfony/Console component.

Beyond the features that symfony/console already provided, the CLIFramework table generator can help you customize the table cell styling, for example, cell alignment, handling text overflow, background color, foreground color, text format, etc.

Getting Started

See the code example below for a simple example:

use CLIFramework\Component\Table\Table;

$table = new Table;
$table->setHeaders([ 'Published Date', 'Title', 'Description' ]);
$table->addRow([
    "September 16, 2014",
    "Zero to One: Notes on Startups, or How to Build the Future",
    29.5
]);
$table->addRow([
    "November 4, 2014",
    "Hooked: How to Build Habit-Forming Products",
    99,
]);
echo $table->render();

Which outputs:

+--------------------+-------------------------------------------------+------+
| Published Date     | Title                                           |      |
+--------------------+-------------------------------------------------+------+
| September 16, 2014 | Zero to One: Notes on Startups, or How to Build | 29.5 |
|                    | the Future                                      |      |
| November 4, 2014   | Hooked: How to Build Habit-Forming Products     | 99   |
+--------------------+-------------------------------------------------+------+

Table options

Use $table->setStyle(new TableStyle()) to modify the appearance of the table. You can extend from TableStyle to create your own appearance. Built-in styles:

TableStyle

+---------+---------+
| header1 | header2 |
+---------+---------+
| value1  | value2  |
| value3  | value4  |
+---------+---------+

BorderlessTableStyle

  header1   header2

  value1    value2
  value3    value4

CompactTableStyle

  header1   header2
---------------------
  value1    value2
  value3    value4
---------------------

MarkdownTableStyle

| header1 | header2 |
|---------|---------|
| value1  | value2  |
| value3  | value4  |

Also, you can set the maximum column with using $table->setMaxColumnWidth($width).

Header

Set the header using $table->setHeader([$header1, $header2]). This adds a row at the top of the table with a separator below.

Rows

You can add new rows by the addRow method:

$table->addRow([ 'Column 1', 'Column 2', 'Column 3'  ]);

To combine multiple-line into one single column, you can add a newline character between the lines or use an array to separate:

$table->addRow([ 'Column 1', 'Column 2', ['Column 3', 'Extra Line']  ]);

Note that addRow method can take an array of string or a CLIFramework\Component\Table\RowSeparator object. You can generate a row separator by the following statement:

$table->addRow(new RowSeparator);

Footer

Set the footer using $table->setFooter($footer). The footer will be rendered at the bottom of the table. If the footer is a string, it will be rendered as a single cell spanning the width of the entire table with a separator above. If the footer is an array, it will be rendered as an ordinary row.

Cell options

The cell formatting can be customized using a CellAttribute that can be specified as the first argument of a cell array:

$attribute = new CellAttribute;
// Setup cell attributes...
$table->addRow([
    [$attribute, 'Column 1'],
    'Column 1',
    ['Column 1', 'Extra Line']
]);

The following methods are available on CellAttribute:

Method Values Description
setAlignment($alignment) CellAttribute::ALIGN_RIGHT
CellAttribute::ALIGN_LEFT
CellAttribute::ALIGN_CENTER
Sets the alignment of the text within a cell.
setTextOverflow($overflow) CellAttribute::WRAP
CellAttribute::CLIP
CellAttribute::ELLIPSIS
Indicates what needs to happen if the cell value is wider than the max column width.
setForegroundColor($color)
setBackgroundColor($color)
See known colors. Adds color to the cell.

Column options

Instead of specifying the cell attributes on every cell in a column, you can also set the attributes for the entire column with $table->setColumnCellAttribute($colIndex, $attribute).