Skip to content

Commit 9f89556

Browse files
committed
[Console] Updated the table helper page
1 parent 6e0d1c3 commit 9f89556

File tree

1 file changed

+52
-29
lines changed

1 file changed

+52
-29
lines changed

components/console/helpers/table.rst

+52-29
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
Table
2-
=====
1+
Table Helper
2+
============
33

4-
When building a console application it may be useful to display tabular data:
5-
6-
.. code-block:: terminal
7-
8-
+---------------+--------------------------+------------------+
9-
| ISBN | Title | Author |
10-
+---------------+--------------------------+------------------+
11-
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
12-
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
13-
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
14-
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
15-
+---------------+--------------------------+------------------+
16-
17-
.. note::
18-
19-
As an alternative, consider using the
20-
:ref:`SymfonyStyle <symfony-style-content>` to display a table.
4+
When building console applications, Symfony provides several utilities for
5+
rendering tabular data. The simplest option is to use the table methods from
6+
:ref:`Symfony Style <symfony-style-content>`. While convenient, this approach
7+
doesn't allow customization of the table's design. For more control and advanced
8+
features, use the ``Table`` console helper explained in this article.
219

2210
To display a table, use :class:`Symfony\\Component\\Console\\Helper\\Table`,
2311
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::
4836
}
4937
}
5038

39+
This outputs:
40+
41+
.. code-block:: terminal
42+
43+
+---------------+--------------------------+------------------+
44+
| ISBN | Title | Author |
45+
+---------------+--------------------------+------------------+
46+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
47+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
48+
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
49+
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
50+
+---------------+--------------------------+------------------+
51+
52+
Adding Table Separators
53+
-----------------------
54+
5155
You can add a table separator anywhere in the output by passing an instance of
5256
:class:`Symfony\\Component\\Console\\Helper\\TableSeparator` as a row::
5357

@@ -73,6 +77,9 @@ You can add a table separator anywhere in the output by passing an instance of
7377
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
7478
+---------------+--------------------------+------------------+
7579
80+
Adding Table Titles
81+
-------------------
82+
7683
You can optionally display titles at the top and the bottom of the table::
7784

7885
// ...
@@ -92,6 +99,9 @@ You can optionally display titles at the top and the bottom of the table::
9299
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
93100
+---------------+--------- Page 1/2 -------+------------------+
94101
102+
Setting the Column Widths Explicitly
103+
------------------------------------
104+
95105
By default, the width of the columns is calculated automatically based on their
96106
contents. Use the :method:`Symfony\\Component\\Console\\Helper\\Table::setColumnWidths`
97107
method to set the column widths explicitly::
@@ -154,6 +164,9 @@ The output of this command will be:
154164
| (the rest of the rows...) |
155165
+-------+------------+--------------------------------+
156166
167+
Rendering Vertical Tables
168+
-------------------------
169+
157170
By default, table contents are displayed horizontally. You can change this behavior
158171
via the :method:`Symfony\\Component\\Console\\Helper\\Table::setVertical` method::
159172

@@ -179,17 +192,24 @@ The output of this command will be:
179192

180193
Support for vertical rendering was introduced in Symfony 6.1.
181194

195+
Customizing the Table Style
196+
---------------------------
197+
182198
The table style can be changed to any built-in styles via
183199
:method:`Symfony\\Component\\Console\\Helper\\Table::setStyle`::
184200

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

188-
// changes the default style to compact
204+
Built-in Table Styles
205+
~~~~~~~~~~~~~~~~~~~~~
206+
207+
**Compact**::
208+
189209
$table->setStyle('compact');
190210
$table->render();
191211

192-
This code results in:
212+
This outputs:
193213

194214
.. code-block:: terminal
195215
@@ -199,12 +219,12 @@ This code results in:
199219
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
200220
80-902734-1-6 And Then There Were None Agatha Christie
201221
202-
You can also set the style to ``borderless``::
222+
**Borderless**::
203223

204224
$table->setStyle('borderless');
205225
$table->render();
206226

207-
which outputs:
227+
This outputs:
208228

209229
.. code-block:: terminal
210230
@@ -217,12 +237,12 @@ which outputs:
217237
80-902734-1-6 And Then There Were None Agatha Christie
218238
=============== ========================== ==================
219239
220-
You can also set the style to ``box``::
240+
**Box**::
221241

222242
$table->setStyle('box');
223243
$table->render();
224244

225-
which outputs:
245+
This outputs:
226246

227247
.. code-block:: terminal
228248
@@ -235,12 +255,12 @@ which outputs:
235255
│ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │
236256
└───────────────┴──────────────────────────┴──────────────────┘
237257
238-
You can also set the style to ``box-double``::
258+
**Double box**::
239259

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

243-
which outputs:
263+
This outputs:
244264

245265
.. code-block:: terminal
246266
@@ -253,7 +273,10 @@ which outputs:
253273
║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║
254274
╚═══════════════╧══════════════════════════╧══════════════════╝
255275
256-
If the built-in styles do not fit your need, define your own::
276+
Making a Custom Table Style
277+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
278+
279+
If the built-in styles do not fit your needs, define your own::
257280

258281
use Symfony\Component\Console\Helper\TableStyle;
259282

0 commit comments

Comments
 (0)