1
- Table
2
- =====
1
+ Table Helper
2
+ ============
3
3
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.
21
9
22
10
To display a table, use :class: `Symfony\\ Component\\ Console\\ Helper\\ Table `,
23
11
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::
48
36
}
49
37
}
50
38
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
+
51
55
You can add a table separator anywhere in the output by passing an instance of
52
56
:class: `Symfony\\ Component\\ Console\\ Helper\\ TableSeparator ` as a row::
53
57
@@ -61,6 +65,8 @@ You can add a table separator anywhere in the output by passing an instance of
61
65
['80-902734-1-6', 'And Then There Were None', 'Agatha Christie'],
62
66
]);
63
67
68
+ This outputs:
69
+
64
70
.. code-block :: terminal
65
71
66
72
+---------------+--------------------------+------------------+
@@ -73,13 +79,18 @@ You can add a table separator anywhere in the output by passing an instance of
73
79
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
74
80
+---------------+--------------------------+------------------+
75
81
82
+ Adding Table Titles
83
+ -------------------
84
+
76
85
You can optionally display titles at the top and the bottom of the table::
77
86
78
87
// ...
79
88
$table->setHeaderTitle('Books');
80
89
$table->setFooterTitle('Page 1/2');
81
90
$table->render();
82
91
92
+ This outputs:
93
+
83
94
.. code-block :: terminal
84
95
85
96
+---------------+----------- Books --------+------------------+
@@ -92,6 +103,9 @@ You can optionally display titles at the top and the bottom of the table::
92
103
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
93
104
+---------------+--------- Page 1/2 -------+------------------+
94
105
106
+ Setting the Column Widths Explicitly
107
+ ------------------------------------
108
+
95
109
By default, the width of the columns is calculated automatically based on their
96
110
contents. Use the :method: `Symfony\\ Component\\ Console\\ Helper\\ Table::setColumnWidths `
97
111
method to set the column widths explicitly::
@@ -114,7 +128,7 @@ argument is the column width::
114
128
$table->setColumnWidth(2, 30);
115
129
$table->render();
116
130
117
- The output of this command will be :
131
+ This outputs :
118
132
119
133
.. code-block :: terminal
120
134
@@ -141,7 +155,7 @@ If you prefer to wrap long contents in multiple rows, use the
141
155
$table->setColumnMaxWidth(1, 10);
142
156
$table->render();
143
157
144
- The output of this command will be :
158
+ This outputs :
145
159
146
160
.. code-block :: terminal
147
161
@@ -154,14 +168,17 @@ The output of this command will be:
154
168
| (the rest of the rows...) |
155
169
+-------+------------+--------------------------------+
156
170
171
+ Rendering Vertical Tables
172
+ -------------------------
173
+
157
174
By default, table contents are displayed horizontally. You can change this behavior
158
175
via the :method: `Symfony\\ Component\\ Console\\ Helper\\ Table::setVertical ` method::
159
176
160
177
// ...
161
178
$table->setVertical();
162
179
$table->render();
163
180
164
- The output of this command will be :
181
+ This outputs :
165
182
166
183
.. code-block :: terminal
167
184
@@ -175,17 +192,24 @@ The output of this command will be:
175
192
| Author: Charles Dickens |
176
193
+------------------------------+
177
194
195
+ Customizing the Table Style
196
+ ---------------------------
197
+
178
198
The table style can be changed to any built-in styles via
179
199
:method: `Symfony\\ Component\\ Console\\ Helper\\ Table::setStyle `::
180
200
181
- // same as calling nothing
201
+ // this 'default' style is the one used when no style is specified
182
202
$table->setStyle('default');
183
203
184
- // changes the default style to compact
204
+ Built-in Table Styles
205
+ ~~~~~~~~~~~~~~~~~~~~~
206
+
207
+ **Compact **::
208
+
185
209
$table->setStyle('compact');
186
210
$table->render();
187
211
188
- This code results in :
212
+ This outputs :
189
213
190
214
.. code-block :: terminal
191
215
@@ -195,12 +219,12 @@ This code results in:
195
219
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
196
220
80-902734-1-6 And Then There Were None Agatha Christie
197
221
198
- You can also set the style to `` borderless `` ::
222
+ ** Borderless ** ::
199
223
200
224
$table->setStyle('borderless');
201
225
$table->render();
202
226
203
- which outputs:
227
+ This outputs:
204
228
205
229
.. code-block :: terminal
206
230
@@ -213,12 +237,12 @@ which outputs:
213
237
80-902734-1-6 And Then There Were None Agatha Christie
214
238
=============== ========================== ==================
215
239
216
- You can also set the style to `` box `` ::
240
+ ** Box ** ::
217
241
218
242
$table->setStyle('box');
219
243
$table->render();
220
244
221
- which outputs:
245
+ This outputs:
222
246
223
247
.. code-block :: terminal
224
248
@@ -231,12 +255,12 @@ which outputs:
231
255
│ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │
232
256
└───────────────┴──────────────────────────┴──────────────────┘
233
257
234
- You can also set the style to `` box-double `` ::
258
+ ** Double box** ::
235
259
236
260
$table->setStyle('box-double');
237
261
$table->render();
238
262
239
- which outputs:
263
+ This outputs:
240
264
241
265
.. code-block :: terminal
242
266
@@ -249,7 +273,10 @@ which outputs:
249
273
║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║
250
274
╚═══════════════╧══════════════════════════╧══════════════════╝
251
275
252
- 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::
253
280
254
281
use Symfony\Component\Console\Helper\TableStyle;
255
282
@@ -339,7 +366,7 @@ To make a table cell that spans multiple columns you can use a :class:`Symfony\\
339
366
;
340
367
$table->render();
341
368
342
- This results in :
369
+ This outputs :
343
370
344
371
.. code-block :: terminal
345
372
@@ -362,7 +389,7 @@ This results in:
362
389
]);
363
390
// ...
364
391
365
- This generates :
392
+ This outputs :
366
393
367
394
.. code-block :: terminal
368
395
@@ -441,7 +468,7 @@ The only requirement to append rows is that the table must be rendered inside a
441
468
}
442
469
}
443
470
444
- This will display the following table in the terminal :
471
+ This outputs :
445
472
446
473
.. code-block :: terminal
447
474
@@ -462,7 +489,7 @@ This will display the following table in the terminal:
462
489
$table->render();
463
490
// ...
464
491
465
- This will display :
492
+ This outputs :
466
493
467
494
.. code-block :: terminal
468
495
0 commit comments