Via Composer
$ composer require shapecode/twig-collector-extension
Add Extension to your Twig Environment
<?php
use Twig\Environment;
use Shapecode\Twig\Extensions\Extension\CollectorExtension;
$twig = new Environment($loader);
$twig->addExtension(new CollectorExtension());
collect stuff over all templates
{% collector stylesheets %}
<style>
.body {
margin-bottom: 10px;
}
</style>
{% endcollector %}
... and output it at a specific line
{% collection stylesheets %}