Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,11 @@ | ||
Table Helper | ||
============ | ||
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:: | ||
@@ -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:: | ||
@@ -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 | ||
+---------------+--------------------------+------------------+ | ||
@@ -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 --------+------------------+ | ||
@@ -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:: | ||
@@ -114,7 +128,7 @@ argument is the column width:: | ||
$table->setColumnWidth(2, 30); | ||
$table->render(); | ||
This outputs: | ||
.. code-block:: terminal | ||
@@ -141,7 +155,7 @@ If you prefer to wrap long contents in multiple rows, use the | ||
$table->setColumnMaxWidth(1, 10); | ||
$table->render(); | ||
This outputs: | ||
.. code-block:: terminal | ||
@@ -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(); | ||
This outputs: | ||
.. code-block:: terminal | ||
@@ -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`:: | ||
//this 'default' style is the one used when no style is specified | ||
$table->setStyle('default'); | ||
Built-in Table Styles | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
**Compact**:: | ||
$table->setStyle('compact'); | ||
$table->render(); | ||
Thisoutputs: | ||
Comment on lines -192 to +216 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Nice catch, streamlining those things would be nice. WDYT @symfony/docs ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Good catch! I made all this consistent and used | ||
.. code-block:: terminal | ||
@@ -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 | ||
**Borderless**:: | ||
$table->setStyle('borderless'); | ||
$table->render(); | ||
This outputs: | ||
.. code-block:: terminal | ||
@@ -217,12 +241,12 @@ which outputs: | ||
80-902734-1-6 And Then There Were None Agatha Christie | ||
=============== ========================== ================== | ||
**Box**:: | ||
$table->setStyle('box'); | ||
$table->render(); | ||
This outputs: | ||
.. code-block:: terminal | ||
@@ -235,12 +259,12 @@ which outputs: | ||
│ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │ | ||
└───────────────┴──────────────────────────┴──────────────────┘ | ||
**Doublebox**:: | ||
$table->setStyle('box-double'); | ||
$table->render(); | ||
This outputs: | ||
.. code-block:: terminal | ||
@@ -253,7 +277,10 @@ which outputs: | ||
║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║ | ||
╚═══════════════╧══════════════════════════╧══════════════════╝ | ||
Making a Custom Table Style | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
If the built-in styles do not fit your needs, define your own:: | ||
use Symfony\Component\Console\Helper\TableStyle; | ||
@@ -343,7 +370,7 @@ To make a table cell that spans multiple columns you can use a :class:`Symfony\\ | ||
; | ||
$table->render(); | ||
Thisoutputs: | ||
.. code-block:: terminal | ||
@@ -366,7 +393,7 @@ This results in: | ||
]); | ||
// ... | ||
Thisoutputs: | ||
.. code-block:: terminal | ||
@@ -445,7 +472,7 @@ The only requirement to append rows is that the table must be rendered inside a | ||
} | ||
} | ||
Thisoutputs: | ||
.. code-block:: terminal | ||
@@ -466,7 +493,7 @@ This will display the following table in the terminal: | ||
$table->render(); | ||
// ... | ||
Thisoutputs: | ||
.. code-block:: terminal | ||