Skip to content

Table Helper

Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.2 (the current stable version).

2.3

The table helper was introduced in Symfony 2.3.

Caution

The Table Helper was deprecated in Symfony 2.5 and will be removed in Symfony 3.0. You should now use the Table class instead which is more powerful.

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 the TableHelper, set headers, rows and render:

1
2
3
4
5
6
7
8
9
10
11
$table = $this->getHelper('table');
$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($output);

The table layout can be customized as well. There are two ways to customize table rendering: using named layouts or by customizing rendering options.

Customize Table Layout using Named Layouts

The Table helper ships with three preconfigured table layouts:

  • TableHelper::LAYOUT_DEFAULT
  • TableHelper::LAYOUT_BORDERLESS
  • TableHelper::LAYOUT_COMPACT

Layout can be set using setLayout() method.

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