The Symfony Console component is the second most popular Symfony component,
with more than eight million downloads on Packagist.org so far. One of the main
factors of its success is the amount of features that it provides to create
advanced console commands. In Symfony 2.7 we went a step further and added support
for defining advanced table layouts.
Added support for colspan and rowspan
Similarly to the well-known properties of the HTML tables, the new colspan
and rowspan
properties allow you to create advanced table layouts. These
properties are supported by the new TableCell
class, which must be used
to define non-regular table layouts.
1
2
3
4
5
6
7
8
9
10
11
12
13 | // A table using colspanuseSymfony\Component\Console\Helper\Table;$table=newTable($output);$table->setHeaders(array('Column #1 title','Column #2 title','Column #3 title'))->setRows(array(array('Value #1','Value #2','Value #3'),newTableSeparator(),array(newTableCell('This value spans 3 columns.',array('colspan'=>3))),));$table->render(); |
| +-----------------+-----------------+------------------+
| Column #1 title | Column #2 title | Column #3 title |
+-----------------+-----------------+------------------+
| Value #1 | Value #2 | Value #3 |
+-----------------+-----------------+------------------+
| This value spans 3 columns. |
+-----------------+-----------------+------------------+ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | // A table using rowspanuseSymfony\Component\Console\Helper\Table;$table=newTable($output);$table->setHeaders(array('Column #1 title','Column #2 title','Column #3 title'))->setRows(array(array(newTableCell('Value #1 spanning two rows.',array('rowspan'=>2)),'Value #2 - 1','Value #3 - 1',),array('Value #2 - 2','Value #3 - 2'),));$table->render(); |
| +-------------------+-----------------+------------------+
| Column #1 title | Column #2 title | Column #3 title |
+-------------------+-----------------+------------------+
| Value #1 spanning | Value #2 - 1 | Value #3 - 1 |
| two rows. | Value #2 - 2 | Value #3 - 2 |
+-------------------+-----------------+------------------+ |
These two properties can also be combined in the same table, allowing to create
any layout that you can imagine. Check out the tests of the Table helper to
view more usage examples.