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 colspan
use Symfony\Component\Console\Helper\Table;
$table = new Table($output);
$table
->setHeaders(array('Column #1 title', 'Column #2 title', 'Column #3 title'))
->setRows(array(
array('Value #1', 'Value #2', 'Value #3'),
new TableSeparator(),
array(new TableCell('This value spans 3 columns.', array('colspan' => 3))),
))
;
$table->render();
1 2 3 4 5 6 7
+-----------------+-----------------+------------------+
| 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 rowspan
use Symfony\Component\Console\Helper\Table;
$table = new Table($output);
$table
->setHeaders(array('Column #1 title', 'Column #2 title', 'Column #3 title'))
->setRows(array(
array(
new TableCell('Value #1 spanning two rows.', array('rowspan' => 2)),
'Value #2 - 1',
'Value #3 - 1',
),
array('Value #2 - 2', 'Value #3 - 2'),
))
;
$table->render();
1 2 3 4 5 6
+-------------------+-----------------+------------------+
| 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.
Added support for multiple headers
A nice side-effect of adding support for colspan
and rowspan
properties
is that tables now support headers of several lines, which are ideal to display
a general table title and some specific column titles:
1 2 3 4 5 6 7 8 9 10 11 12 13
use Symfony\Component\Console\Helper\Table;
$table = new Table($output);
$table
->setHeaders(array(
array(new TableCell('Main table title', array('colspan' => 3))),
array('Column #1 title', 'Column #2 title', 'Column #3 title'),
))
->setRows(array(
// ...
))
;
$table->render();
1 2 3 4 5 6 7
+-----------------+-----------------+-----------------+
| Main table title |
+-----------------+-----------------+-----------------+
| Column #1 title | Column #2 title | Column #3 title |
+-----------------+-----------------+-----------------+
| ... |
+-----------------+-----------------+-----------------+
why are you using the deprecated API of the TableHelper in the code snippets instead of using the 2.5+ API with the Table class ?
@stof good point! I've just updated the sample code showed in the blog post. Thanks.
Nice feature. Thanks !
Great Feature! I use CLI a lot!
Just wondering, do you have any real use life examples? I'd like to get inspired.
The second table in this image is a real example of rowspan: http://www.gridgain.com/wp-content/uploads/2014/06/cli_4-670x498.png
And here you have a real example of multiple headers table: http://cloudacademy.com/blog/wp-content/uploads/2014/09/54103174d287c25dd50a71ed.png
Great job. Thanks.
+1