Quantcast
Channel: Symfony Blog
Viewing all articles
Browse latest Browse all 3059

New in Symfony 2.7: Advanced table layouts for console commands

$
0
0
Ait Boudad Abdellatif

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 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();
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 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();
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
useSymfony\Component\Console\Helper\Table;$table=newTable($output);$table->setHeaders(array(array(newTableCell('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 |
+-----------------+-----------------+-----------------+
| ...                                                 |
+-----------------+-----------------+-----------------+

Be trained by Symfony experts - 2015-03-30 Clichy - 2015-04-10 Paris - 2015-04-10 Paris

Viewing all articles
Browse latest Browse all 3059

Trending Articles