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
@@ -73,6 +77,9 @@ You can add a table separator anywhere in the output by passing an instance of
73
77
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
74
78
+---------------+--------------------------+------------------+
75
79
80
+ Adding Table Titles
81
+ -------------------
82
+
76
83
You can optionally display titles at the top and the bottom of the table::
77
84
78
85
// ...
@@ -92,6 +99,9 @@ You can optionally display titles at the top and the bottom of the table::
92
99
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
93
100
+---------------+--------- Page 1/2 -------+------------------+
94
101
102
+ Setting the Column Widths Explicitly
103
+ ------------------------------------
104
+
95
105
By default, the width of the columns is calculated automatically based on their
96
106
contents. Use the :method: `Symfony\\ Component\\ Console\\ Helper\\ Table::setColumnWidths `
97
107
method to set the column widths explicitly::
@@ -154,6 +164,9 @@ The output of this command will be:
154
164
| (the rest of the rows...) |
155
165
+-------+------------+--------------------------------+
156
166
167
+ Rendering Vertical Tables
168
+ -------------------------
169
+
157
170
By default, table contents are displayed horizontally. You can change this behavior
158
171
via the :method: `Symfony\\ Component\\ Console\\ Helper\\ Table::setVertical ` method::
159
172
@@ -179,17 +192,24 @@ The output of this command will be:
179
192
180
193
Support for vertical rendering was introduced in Symfony 6.1.
181
194
195
+ Customizing the Table Style
196
+ ---------------------------
197
+
182
198
The table style can be changed to any built-in styles via
183
199
:method: `Symfony\\ Component\\ Console\\ Helper\\ Table::setStyle `::
184
200
185
- // same as calling nothing
201
+ // this 'default' style is the one used when no style is specified
186
202
$table->setStyle('default');
187
203
188
- // changes the default style to compact
204
+ Built-in Table Styles
205
+ ~~~~~~~~~~~~~~~~~~~~~
206
+
207
+ **Compact **::
208
+
189
209
$table->setStyle('compact');
190
210
$table->render();
191
211
192
- This code results in :
212
+ This outputs :
193
213
194
214
.. code-block :: terminal
195
215
@@ -199,12 +219,12 @@ This code results in:
199
219
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
200
220
80-902734-1-6 And Then There Were None Agatha Christie
201
221
202
- You can also set the style to `` borderless `` ::
222
+ ** Borderless ** ::
203
223
204
224
$table->setStyle('borderless');
205
225
$table->render();
206
226
207
- which outputs:
227
+ This outputs:
208
228
209
229
.. code-block :: terminal
210
230
@@ -217,12 +237,12 @@ which outputs:
217
237
80-902734-1-6 And Then There Were None Agatha Christie
218
238
=============== ========================== ==================
219
239
220
- You can also set the style to `` box `` ::
240
+ ** Box ** ::
221
241
222
242
$table->setStyle('box');
223
243
$table->render();
224
244
225
- which outputs:
245
+ This outputs:
226
246
227
247
.. code-block :: terminal
228
248
@@ -235,12 +255,12 @@ which outputs:
235
255
│ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │
236
256
└───────────────┴──────────────────────────┴──────────────────┘
237
257
238
- You can also set the style to `` box-double `` ::
258
+ ** Double box** ::
239
259
240
260
$table->setStyle('box-double');
241
261
$table->render();
242
262
243
- which outputs:
263
+ This outputs:
244
264
245
265
.. code-block :: terminal
246
266
@@ -253,7 +273,10 @@ which outputs:
253
273
║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║
254
274
╚═══════════════╧══════════════════════════╧══════════════════╝
255
275
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::
257
280
258
281
use Symfony\Component\Console\Helper\TableStyle;
259
282
0 commit comments