English spoken conference

Symfony 5: The Fast Track

A new book to learn about developing modern Symfony 5 applications.

Support this project

WARNING: You are browsing the documentation for Symfony 2.6 which is not maintained anymore. Consider upgrading your projects to Symfony 4.3.

Table

TableΒΆ

New in version 2.5: The Table class was introduced in Symfony 2.5 as a replacement for the Table Helper.

When building a console application it may be useful to display tabular data:

1
2
3
4
5
6
7
8
+---------------+--------------------------+------------------+
| ISBN          | Title                    | Author           |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy            | Dante Alighieri  |
| 9971-5-0210-0 | A Tale of Two Cities     | Charles Dickens  |
| 960-425-059-0 | The Lord of the Rings    | J. R. R. Tolkien |
| 80-902734-1-6 | And Then There Were None | Agatha Christie  |
+---------------+--------------------------+------------------+

To display a table, use Table, set the headers, set the rows and then render the table:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
use Symfony\Component\Console\Helper\Table;
// ...

class SomeCommand extends Command
{
    public function execute(InputInterface $input, OutputInterface $output)
    {
        $table = new Table($output);
        $table
            ->setHeaders(array('ISBN', 'Title', 'Author'))
            ->setRows(array(
                array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
                array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
                array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
                array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
            ))
        ;
        $table->render();
    }
}

You can add a table separator anywhere in the output by passing an instance of TableSeparator as a row:

1
2
3
4
5
6
7
8
9
use Symfony\Component\Console\Helper\TableSeparator;

$table->setRows(array(
    array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
    array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
    new TableSeparator(),
    array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
    array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
));
1
2
3
4
5
6
7
8
9
+---------------+--------------------------+------------------+
| ISBN          | Title                    | Author           |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy            | Dante Alighieri  |
| 9971-5-0210-0 | A Tale of Two Cities     | Charles Dickens  |
+---------------+--------------------------+------------------+
| 960-425-059-0 | The Lord of the Rings    | J. R. R. Tolkien |
| 80-902734-1-6 | And Then There Were None | Agatha Christie  |
+---------------+--------------------------+------------------+

The table style can be changed to any built-in styles via setStyle():

1
2
3
4
5
6
// same as calling nothing
$table->setStyle('default');

// changes the default style to compact
$table->setStyle('compact');
$table->render();

This code results in:

1
2
3
4
5
ISBN          Title                    Author
99921-58-10-7 Divine Comedy            Dante Alighieri
9971-5-0210-0 A Tale of Two Cities     Charles Dickens
960-425-059-0 The Lord of the Rings    J. R. R. Tolkien
80-902734-1-6 And Then There Were None Agatha Christie

You can also set the style to borderless:

1
2
$table->setStyle('borderless');
$table->render();

which outputs:

1
2
3
4
5
6
7
8
=============== ========================== ==================
 ISBN            Title                      Author
=============== ========================== ==================
 99921-58-10-7   Divine Comedy              Dante Alighieri
 9971-5-0210-0   A Tale of Two Cities       Charles Dickens
 960-425-059-0   The Lord of the Rings      J. R. R. Tolkien
 80-902734-1-6   And Then There Were None   Agatha Christie
=============== ========================== ==================

If the built-in styles do not fit your need, define your own:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
use Symfony\Component\Console\Helper\TableStyle;

// by default, this is based on the default style
$style = new TableStyle();

// customize the style
$style
    ->setHorizontalBorderChar('<fg=magenta>|</>')
    ->setVerticalBorderChar('<fg=magenta>-</>')
    ->setCrossingChar(' ')
;

// use the style for this table
$table->setStyle($style);

Here is a full list of things you can customize:

Tip

You can also register a style globally:

1
2
3
4
5
// register the style under the colorful name
Table::setStyleDefinition('colorful', $style);

// use it for a table
$table->setStyle('colorful');

This method can also be used to override a built-in style.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.