Compressing linked JavaScript and CSS files is no big deal when using Assetic in my Symfony 2.8 project. But what about scripts that are directly embedded in the page/template using a script tag. These scripts are not modified in any way.
Is it possible to compress/modify/uglify these scripts as well?
Of course I could simply move these scripts into separate files and thus apply the Assetic filters to them as well. But in some cases it is just handy to have the scripts directly within the HTML / Twig template.
Does Twig provide any solution to filter these scripts without moving them?
You could build a custom tag doing it during the template rendering. But be careful about the performance impact, as it might not end up faster than downloading a slightly bigger HTML response if you do it on rendering.
If you have a big script tag, moving it to a separate file is better for caching (and for compressing it at build time instead.
I guess Twig will never include this. However, you can easily create your own solution. In one application I use the following code:
<style>
{{ minify(include('css/homepage.css')) }}
</style>
Instead of include() you can also use source(). And the minify() custom function is just a wrapper of the https://github.com/matthiasmullie/minify library:
use MatthiasMullie\Minify\CSS;
new \Twig_SimpleFunction('minify', [$this, 'minifyCss'], ['is_safe' => ['html']]),
public function minifyCss($content)
{
$minifier = new CSS();
$minifier->add($content);
return $minifier->minify();
}
Closing as this is feature that belongs to Twig core.