Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Update the table helper page #20941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 63 additions & 36 deletions components/console/helpers/table.rst
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
Table
=====
Table Helper
============

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

.. code-block:: terminal

+---------------+--------------------------+------------------+
| 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 |
+---------------+--------------------------+------------------+

.. note::

As an alternative, consider using the
:ref:`SymfonyStyle <symfony-style-content>` to display a table.
When building console applications, Symfony provides several utilities for
rendering tabular data. The simplest option is to use the table methods from
:ref:`Symfony Style <symfony-style-content>`. While convenient, this approach
doesn't allow customization of the table's design. For more control and advanced
features, use the ``Table`` console helper explained in this article.

To display a table, use :class:`Symfony\\Component\\Console\\Helper\\Table`,
set the headers, set the rows and then render the table::
Expand Down Expand Up @@ -48,6 +36,22 @@ set the headers, set the rows and then render the table::
}
}

This outputs:

.. code-block:: terminal

+---------------+--------------------------+------------------+
| 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 |
+---------------+--------------------------+------------------+

Adding Table Separators
-----------------------

You can add a table separator anywhere in the output by passing an instance of
:class:`Symfony\\Component\\Console\\Helper\\TableSeparator` as a row::

Expand All @@ -61,6 +65,8 @@ You can add a table separator anywhere in the output by passing an instance of
['80-902734-1-6', 'And Then There Were None', 'Agatha Christie'],
]);

This outputs:

.. code-block:: terminal

+---------------+--------------------------+------------------+
Expand All @@ -73,13 +79,18 @@ You can add a table separator anywhere in the output by passing an instance of
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
+---------------+--------------------------+------------------+

Adding Table Titles
-------------------

You can optionally display titles at the top and the bottom of the table::

// ...
$table->setHeaderTitle('Books');
$table->setFooterTitle('Page 1/2');
$table->render();

This outputs:

.. code-block:: terminal

+---------------+----------- Books --------+------------------+
Expand All @@ -92,6 +103,9 @@ You can optionally display titles at the top and the bottom of the table::
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
+---------------+--------- Page 1/2 -------+------------------+

Setting the Column Widths Explicitly
------------------------------------

By default, the width of the columns is calculated automatically based on their
contents. Use the :method:`Symfony\\Component\\Console\\Helper\\Table::setColumnWidths`
method to set the column widths explicitly::
Expand All @@ -114,7 +128,7 @@ argument is the column width::
$table->setColumnWidth(2, 30);
$table->render();

The output of this command will be:
This outputs:

.. code-block:: terminal

Expand All @@ -141,7 +155,7 @@ If you prefer to wrap long contents in multiple rows, use the
$table->setColumnMaxWidth(1, 10);
$table->render();

The output of this command will be:
This outputs:

.. code-block:: terminal

Expand All @@ -154,14 +168,17 @@ The output of this command will be:
| (the rest of the rows...) |
+-------+------------+--------------------------------+

Rendering Vertical Tables
-------------------------

By default, table contents are displayed horizontally. You can change this behavior
via the :method:`Symfony\\Component\\Console\\Helper\\Table::setVertical` method::

// ...
$table->setVertical();
$table->render();

The output of this command will be:
This outputs:

.. code-block:: terminal

Expand All @@ -179,17 +196,24 @@ The output of this command will be:

Support for vertical rendering was introduced in Symfony 6.1.

Customizing the Table Style
---------------------------

The table style can be changed to any built-in styles via
:method:`Symfony\\Component\\Console\\Helper\\Table::setStyle`::

// same as calling nothing
// this 'default' style is the one used when no style is specified
$table->setStyle('default');

// changes the default style to compact
Built-in Table Styles
~~~~~~~~~~~~~~~~~~~~~

**Compact**::

$table->setStyle('compact');
$table->render();

This code results in:
This outputs:
Comment on lines -192 to +216
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw we don't have same wording for result in this file, should we also update them ?

This outputs:
The output of this command will be:
This generates:
This will display the following table in the terminal:
This will display:
Nothing for Adding Table Titles section

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, streamlining those things would be nice.

WDYT @symfony/docs ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I made all this consistent and used This outputs because it's very concise and easy to understand. Thanks!


.. code-block:: terminal

Expand All @@ -199,12 +223,12 @@ This code results in:
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``::
**Borderless**::

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

which outputs:
This outputs:

.. code-block:: terminal

Expand All @@ -217,12 +241,12 @@ which outputs:
80-902734-1-6 And Then There Were None Agatha Christie
=============== ========================== ==================

You can also set the style to ``box``::
**Box**::

$table->setStyle('box');
$table->render();

which outputs:
This outputs:

.. code-block:: terminal

Expand All @@ -235,12 +259,12 @@ which outputs:
│ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │
└───────────────┴──────────────────────────┴──────────────────┘

You can also set the style to ``box-double``::
**Double box**::

$table->setStyle('box-double');
$table->render();

which outputs:
This outputs:

.. code-block:: terminal

Expand All @@ -253,7 +277,10 @@ which outputs:
║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║
╚═══════════════╧══════════════════════════╧══════════════════╝

If the built-in styles do not fit your need, define your own::
Making a Custom Table Style
~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

use Symfony\Component\Console\Helper\TableStyle;

Expand Down Expand Up @@ -343,7 +370,7 @@ To make a table cell that spans multiple columns you can use a :class:`Symfony\\
;
$table->render();

This results in:
This outputs:

.. code-block:: terminal

Expand All @@ -366,7 +393,7 @@ This results in:
]);
// ...

This generates:
This outputs:

.. code-block:: terminal

Expand Down Expand Up @@ -445,7 +472,7 @@ The only requirement to append rows is that the table must be rendered inside a
}
}

This will display the following table in the terminal:
This outputs:

.. code-block:: terminal

Expand All @@ -466,7 +493,7 @@ This will display the following table in the terminal:
$table->render();
// ...

This will display:
This outputs:

.. code-block:: terminal

Expand Down