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
2210To display a table, use :class: `Symfony\\ Component\\ Console\\ Helper\\ Table `,
2311set 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+
5155You 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
@@ -61,6 +65,8 @@ You can add a table separator anywhere in the output by passing an instance of
6165 ['80-902734-1-6', 'And Then There Were None', 'Agatha Christie'],
6266 ]);
6367
68+ This outputs:
69+
6470.. code-block :: terminal
6571
6672 +---------------+--------------------------+------------------+
@@ -73,13 +79,18 @@ You can add a table separator anywhere in the output by passing an instance of
7379 | 80-902734-1-6 | And Then There Were None | Agatha Christie |
7480 +---------------+--------------------------+------------------+
7581
82+ Adding Table Titles
83+ -------------------
84+
7685You can optionally display titles at the top and the bottom of the table::
7786
7887 // ...
7988 $table->setHeaderTitle('Books');
8089 $table->setFooterTitle('Page 1/2');
8190 $table->render();
8291
92+ This outputs:
93+
8394.. code-block :: terminal
8495
8596 +---------------+----------- Books --------+------------------+
@@ -92,6 +103,9 @@ You can optionally display titles at the top and the bottom of the table::
92103 | 80-902734-1-6 | And Then There Were None | Agatha Christie |
93104 +---------------+--------- Page 1/2 -------+------------------+
94105
106+ Setting the Column Widths Explicitly
107+ ------------------------------------
108+
95109By default, the width of the columns is calculated automatically based on their
96110contents. Use the :method: `Symfony\\ Component\\ Console\\ Helper\\ Table::setColumnWidths `
97111method to set the column widths explicitly::
@@ -114,7 +128,7 @@ argument is the column width::
114128 $table->setColumnWidth(2, 30);
115129 $table->render();
116130
117- The output of this command will be :
131+ This outputs :
118132
119133.. code-block :: terminal
120134
@@ -141,7 +155,7 @@ If you prefer to wrap long contents in multiple rows, use the
141155 $table->setColumnMaxWidth(1, 10);
142156 $table->render();
143157
144- The output of this command will be :
158+ This outputs :
145159
146160.. code-block :: terminal
147161
@@ -154,14 +168,17 @@ The output of this command will be:
154168 | (the rest of the rows...) |
155169 +-------+------------+--------------------------------+
156170
171+ Rendering Vertical Tables
172+ -------------------------
173+
157174By default, table contents are displayed horizontally. You can change this behavior
158175via the :method: `Symfony\\ Component\\ Console\\ Helper\\ Table::setVertical ` method::
159176
160177 // ...
161178 $table->setVertical();
162179 $table->render();
163180
164- The output of this command will be :
181+ This outputs :
165182
166183.. code-block :: terminal
167184
@@ -175,37 +192,24 @@ The output of this command will be:
175192 | Author: Charles Dickens |
176193 +------------------------------+
177194
195+ Customizing the Table Style
196+ ---------------------------
197+
178198The table style can be changed to any built-in styles via
179199:method: `Symfony\\ Component\\ Console\\ Helper\\ Table::setStyle `::
180200
181- // same as calling nothing
201+ // this 'default' style is the one used when no style is specified
182202 $table->setStyle('default');
183203
184- // changes the default style to markdown
185- $table->setStyle('markdown');
186- $table->render();
204+ Built-in Table Styles
205+ ~~~~~~~~~~~~~~~~~~~~~
187206
188- This outputs the table in the Markdown format:
189-
190- .. code-block :: terminal
191-
192- | ISBN | Title | Author |
193- |---------------|--------------------------|------------------|
194- | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
195- | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
196- | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
197- | 80-902734-1-6 | And Then There Were None | Agatha Christie |
198-
199- .. versionadded :: 7.3
200-
201- The ``markdown `` style was introduced in Symfony 7.3.
202-
203- You can also set the style to ``compact ``::
207+ **Compact **::
204208
205209 $table->setStyle('compact');
206210 $table->render();
207211
208- The output of this command will be :
212+ This outputs :
209213
210214.. code-block :: terminal
211215
@@ -215,12 +219,12 @@ The output of this command will be:
215219 960-425-059-0 The Lord of the Rings J. R. R. Tolkien
216220 80-902734-1-6 And Then There Were None Agatha Christie
217221
218- You can also set the style to `` borderless `` ::
222+ ** Borderless ** ::
219223
220224 $table->setStyle('borderless');
221225 $table->render();
222226
223- which outputs:
227+ This outputs:
224228
225229.. code-block :: terminal
226230
@@ -233,12 +237,12 @@ which outputs:
233237 80-902734-1-6 And Then There Were None Agatha Christie
234238 =============== ========================== ==================
235239
236- You can also set the style to `` box `` ::
240+ ** Box ** ::
237241
238242 $table->setStyle('box');
239243 $table->render();
240244
241- which outputs:
245+ This outputs:
242246
243247.. code-block :: terminal
244248
@@ -251,12 +255,12 @@ which outputs:
251255 │ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │
252256 └───────────────┴──────────────────────────┴──────────────────┘
253257
254- You can also set the style to `` box-double `` ::
258+ ** Double box** ::
255259
256260 $table->setStyle('box-double');
257261 $table->render();
258262
259- which outputs:
263+ This outputs:
260264
261265.. code-block :: terminal
262266
@@ -269,7 +273,30 @@ which outputs:
269273 ║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║
270274 ╚═══════════════╧══════════════════════════╧══════════════════╝
271275
272- If the built-in styles do not fit your need, define your own::
276+ **Markdown **::
277+
278+ $table->setStyle('markdown');
279+ $table->render();
280+
281+ This outputs:
282+
283+ .. code-block :: terminal
284+
285+ | ISBN | Title | Author |
286+ |---------------|--------------------------|------------------|
287+ | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
288+ | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
289+ | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
290+ | 80-902734-1-6 | And Then There Were None | Agatha Christie |
291+
292+ .. versionadded :: 7.3
293+
294+ The ``markdown `` style was introduced in Symfony 7.3.
295+
296+ Making a Custom Table Style
297+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
298+
299+ If the built-in styles do not fit your needs, define your own::
273300
274301 use Symfony\Component\Console\Helper\TableStyle;
275302
@@ -359,7 +386,7 @@ To make a table cell that spans multiple columns you can use a :class:`Symfony\\
359386 ;
360387 $table->render();
361388
362- This results in :
389+ This outputs :
363390
364391.. code-block :: terminal
365392
@@ -382,7 +409,7 @@ This results in:
382409 ]);
383410 // ...
384411
385- This generates :
412+ This outputs :
386413
387414 .. code-block :: terminal
388415
@@ -461,7 +488,7 @@ The only requirement to append rows is that the table must be rendered inside a
461488 }
462489 }
463490
464- This will display the following table in the terminal :
491+ This outputs :
465492
466493.. code-block :: terminal
467494
@@ -482,7 +509,7 @@ This will display the following table in the terminal:
482509 $table->render();
483510 // ...
484511
485- This will display :
512+ This outputs :
486513
487514 .. code-block :: terminal
488515
0 commit comments