New in Symfony 2.7: Advanced table layouts for console commands
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Abdellatif Ait boudad
in #13438.
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 |
+-----------------+-----------------+-----------------+
| ... |
+-----------------+-----------------+-----------------+
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
And here you have a real example of multiple headers table: http://cloudacademy.com/blog/wp-content/uploads/2014/09/54103174d287c25dd50a71ed.png