New in Symfony 4.4: Horizontal Tables and Definition Lists in Console Commands
October 21, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
The Console component includes some features to output tables and configure them in many different ways. In Symfony 4.4 we've improved it with new types of tables.
Horizontal Tables
Consider the following table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
// ...
class SomeCommand extends Command
{
public function execute(InputInterface $input, OutputInterface $output)
{
$table = new Table($output);
$table
->setHeaders(['ISBN', 'Title', 'Author'])
->setRows([
['99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'],
['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'],
['960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'],
['80-902734-1-6', 'And Then There Were None', 'Agatha Christie'],
])
;
$table->render();
}
}
This is displayed as:
1 2 3 4 5 6 7 8
+---------------+--------------------------+------------------+
| ISBN | Title | Author |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
+---------------+--------------------------+------------------+
If you call to the new setHorizontal(bool $horizontal = true)
method, the
table is transformed into an "horizontal table", where the data is displayed in
rows instead of columns:
1 2 3 4 5 6 7
$table
->setHeaders(['ISBN', 'Title', 'Author'])
->setRows([
// ... the rows ...
])
->setHorizontal()
;
1 2 3 4 5
+--------+-----------------+----------------------+-----------------------+--------------------------+
| ISBN | 99921-58-10-7 | 9971-5-0210-0 | 960-425-059-0 | 80-902734-1-6 |
| Title | Divine Comedy | A Tale of Two Cities | The Lord of the Rings | And Then There Were None |
| Author | Dante Alighieri | Charles Dickens | J. R. R. Tolkien | Agatha Christie |
+--------+-----------------+----------------------+-----------------------+--------------------------+
When using the Symfony style for commands you can also use the
horizontalTable()
shortcut:
1 2 3 4 5 6 7 8 9 10 11 12
use Symfony\Component\Console\Style\SymfonyStyle;
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->horizontalTable(
['ISBN', 'Title', 'Author'],
[
// ... the rows ...
]
);
}
Definition Lists
In Symfony 4.4 we've also added support for "definition lists", which are a set
of key-value pairs, similar to HTML's <dl>
. Symfony's definition lists are
more flexible because they can include titles, separators, etc.
1 2 3 4 5 6 7 8 9 10
use Symfony\Component\Console\Helper\TableSeparator;
$io->definitionList(
['Version' => '4.4.0'],
['Long-Term Support' => 'Yes'],
new TableSeparator(),
'Timeline',
['End of maintenance' => '11/2022'],
['End of life' => '11/2023']
);
This example is output as follows:
1 2 3 4 5 6 7 8
-------------------- ---------
Version 4.4.0
Long-Term Support Yes
-------------------- ---------
Timeline
End of maintenance 11/2022
End of life 11/2023
-------------------- ---------
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 are closed.
To ensure that comments stay relevant, they are closed for old posts.
Cool! Thank you! =)
That's pretty nice!
Hey, it's really nice, thanks!
YESS! This is super useful!! :D I did it manually so many times!!